簡體   English   中英

Java Regex - 匹配不以字符結尾的多行

[英]Java Regex - Match multiple lines that don't end with character

如果數據跨越多行,它將以“ ; ”結尾,但可能會有包含“ ; ”的注釋文本。

This is test
b=4
/* a=b+c; */
b=5
c=7
/* a=b-c; */
a=b*c;

我試過這個正則表達式。

(This is test).+?;

它返回

This is test
b=4
/* a=b+c;

我想知道整個文本匹配的正確正則表達式。

因為您試圖忽略換行符並定義; 作為您的自定義行終止符,最好使用String#split(String) 嘗試以下模式:

\;(?!\s*\*\/)

如果我將它與您的字符串一起使用:

String str = "This is test"
+"\nb=4"
+"\n/* a=b+c; */"
+"\nb=5\nc=7"
+"\n/* a=b-c; */"
+"\na=b*c;";

for(String s : str.split("\\;(?!\\s*\\*\\/)")) {
    System.out.println(s);
}

我得到以下輸出:

This is test
b=4
/* a=b+c; */
b=5
c=7
/* a=b-c; */
a=b*c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM