繁体   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