我有一個字符串涵蓋了多行。我需要提取兩個字符串之間的文本。例如:使用Perl從多行字符串中提取文本
Start Here Some example
text covering a few
lines. End Here
我需要提取字符串,Start Here Some example text covering a few lines.
我怎麼去呢?
我有一個字符串涵蓋了多行。我需要提取兩個字符串之間的文本。例如:使用Perl從多行字符串中提取文本
Start Here Some example
text covering a few
lines. End Here
我需要提取字符串,Start Here Some example text covering a few lines.
我怎麼去呢?
print $1 if /(Start Here.*?)End Here/s;
使用正則表達式/s
修改治療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;
這將捕獲到最後End Here
,萬一出現超過在文本一次。
如果這不是你想要的,那麼你可以使用:
$string =~ /(Start Here.*?)End Here/s;
print $1;
這將停止在End Here
最先出現的匹配。
將正確的修飾符視爲單行而不是(?s)而不是(/ s)?我已經有類似的問題,現在冥思苦想了好一會兒,並嵌入到JMeter的視圖的正則表達式測試儀結果樹監聽器顯示了與正則表達式我正則表達式提取
(?s)<FMSFlightPlan>(.*?)</FMSFlightPlan>
比賽
<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>
而正則表達式
(?s)<FMSFlightPlan>(.*?)</FMSFlightPlan>
不匹配。其他正則表達式測試者顯示相同的結果。然而,當我試圖執行一個腳本,我得到BeanShell的斷言錯誤:
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),
所以別的東西肯定是不對的我的。無論如何,只是一個建議
您正確解釋了所需的's'(dotall)修飾符,但在您的示例中,您正在使用'm'(多行)修飾符。 – stema
@stema:謝謝!這個例子是不正確的...對不起... – sergio
你也使用貪婪的匹配,所以如果有人有說什麼......「開始等等,結束等等等等,開始等等等等結束」,它將捕獲開始/結束序列。如果你使用'。*?'代替,你一次只能限制一次。 – unpythonic