簡體   English   中英

java中的正則表達式匹配

[英]regular expression matching in java

這是我的示例文件:

lineone one
RUN lineone two
lineone three
RUN lineone four

我想選擇所有不以run開頭的行,這是我的做法:

^([^RUN])

是否可以匹配所有不以RUN開頭的行,然后將它們附加到上一行的后面? 像這樣

lineone one RUN lineone two
lineone three RUN lineone four

如果您的示例是正確的,您只需要將"\\nRUN"替換為" RUN"

System.out.println(yourString.replaceAll("\nRUN", " RUN"));

結果:

lineone one RUN lineone two
lineone three RUN lineone four

ideone

首先

^([^RUN]) 

無法正常工作,因為它將匹配任何不以R,U或N開頭的行。

你應該使用先行:

^(?!RUN)

這應該做你想要的:

Pattern p = Pattern.compile("\n(RUN)", Pattern.DOTALL);
Matcher matcher = p.matcher("lineone one\nRUN lineone two\nlineone three\nRUN lineone four");
String replaceAll = matcher.replaceAll(" $1");
System.out.println(replaceAll);

暫無
暫無

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

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