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
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
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
.
print $1 if /(Start Here.*?)End Here/s;