[英]regular expression matching in java
Here is my example file : 这是我的示例文件:
lineone one
RUN lineone two
lineone three
RUN lineone four
I want to select all lines not starting with run, here is how I did it : 我想选择所有不以run开头的行,这是我的做法:
^([^RUN])
Is it possible to match all lines not starting with RUN and then append them to the back of the previous line ? 是否可以匹配所有不以RUN开头的行,然后将它们附加到上一行的后面? like this 像这样
lineone one RUN lineone two
lineone three RUN lineone four
First of all 首先
^([^RUN])
does not work correctly as it will match any line that does not start with either R, U or N. 无法正常工作,因为它将匹配任何不以R,U或N开头的行。
You should use lookahead: 你应该使用先行:
^(?!RUN)
This should do what you want: 这应该做你想要的:
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.