Extract text from a multiline string using Perl

前端 未结 3 1802
太阳男子
太阳男子 2020-12-31 14:41

I have a string that covers several lines. I need to extract the text between two strings. For example:

Start Here Some example
text covering a few
lines. En         


        
相关标签:
3条回答
  • 2020-12-31 15:12

    Wouldn't the correct modifier to treat the string as a single line be (?s) rather than (/s) ? I've been wrestling with a similar problem for quite a while now and the RegExp Tester embedded in JMeter's View Results Tree listener shows my regular expression extractor with the regex

    (?s)<FMSFlightPlan>(.*?)</FMSFlightPlan>
    

    matches

    <FMSFlightPlan>
    C87D
    AN NTEST/GL 
    - FPN/FN/RP:DA:GCRR:AA:EIKN:F:SAMAR,N30540W014249.UN873. 
    BAROK,N35580W010014..PESUL,N40529W008069..RELVA,N41512W008359.. 
    SIVIR,N46000W008450..EMPER,N49000W009000..CON,N53545W008492 
    </FMSFlightPlan>
    

    while the regex

    (?s)<FMSFlightPlan>(.*?)</FMSFlightPlan>
    

    does not match. Other regex testers show the same result. However when I try to execute a the script I get the Beanshell Assertion error:

    Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.io.*; //write out the data results to a file outfile = "/Users/Dani . . . '' Token Parsing Error: Lexical error at line 12, column 380. Encountered: "\n" (10),

    So something else is definitely wrong with mine. Anyway, just a suggestion

    0 讨论(0)
  • 2020-12-31 15:17

    Use the /s regex modifier to treat the string as a single line:

    /s Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

      $string =~ /(Start Here.*)End Here/s;
      print $1;
    

    This will capture up to the last End Here, in case it appears more than once in your text.

    If this is not what you want, then you can use:

      $string =~ /(Start Here.*?)End Here/s;
      print $1;
    

    This will stop matching at the very first occurrence of End Here.

    0 讨论(0)
  • 2020-12-31 15:27
    print $1 if /(Start Here.*?)End Here/s;
    
    0 讨论(0)
提交回复
热议问题