简体   繁体   English

正则表达式以零长度的行开始,并在Java中跨行继续

[英]Regex starting with a zero length line and continuing across lines in Java

I want to match the following: a zero length line, with the match continuing across lines of non-zero length until a particular string is matched in a line. 我想匹配以下内容:零长度的行,匹配持续跨非零长度的行,直到在一行中匹配特定的字符串为止。 Eg: the match starts with a zero length line and continues until STOP is reached: 例如:比赛从零长度线开始,一直持续到到达STOP:

Some random text I don't care about

The match starts at the beginning of this line
The match continues across this line
The match stops here STOP more
text I don't care about

Any suggestions? 有什么建议么?

Thanks 谢谢

This should do it: 应该这样做:

(?ms)^[ \t]*+$\s*+((?:(?!STOP).)*+)

A little demo: 一些演示:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main { 
    public static void main(String[] args) {
        String text = "Some random text I don't care about"      + "\n" +
                ""                                               + "\n" +
                "The match starts at the beginning of this line" + "\n" +
                "The match continues across this line"           + "\n" +
                "The match stops here STOP more"                 + "\n" +
                "don't care about"                               + "\n" +
                ""                                               + "\n" +
                ""                                               + "\n" +
                ""                                               + "\n" +
                "foo"                                            + "\n" +
                "barSTOP"                                        + "\n" +
                "text I don't care about";
        Matcher m = Pattern.compile("(?ms)^[ \t]*+$\\s*+(?:(?!STOP).)*+").matcher(text);
        while(m.find()) {
            System.out.println("match ->"+m.group()+"<-");
        }
    }
}

which will output: 将输出:

match ->
The match starts at the beginning of this line
The match continues across this line
The match stops here <-
match ->


foo
bar<-

A small explanation: 一个小解释:

(?ms)               # enable mutli-line and dot-all
^[ \t]*+$           # match and empty line
\s*+                # match the line break
(                   # start group 1
  (?:(?!STOP).)     #   if the string 'STOP' cannot be seen, match any character
  *+                #   match the previous zero or more times (possessively)
)                   # stop group 1

Making sure you set the "match multi-line" flag, the expression is "\\\\n(\\\\n.*STOP)" . 确保设置“匹配多行”标志,表达式为"\\\\n(\\\\n.*STOP)" The first (and only) match group yields your result. 第一个(也是唯一一个)匹配组产生您的结果。 On DOS and windows systems use "\\\\r\\\\n" in place of "\\\\n" . 在DOS和Windows系统上,使用"\\\\r\\\\n"代替"\\\\n"

(?ms)^$(.+)STOP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM