Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Regex最佳实践_Java_Regex - Fatal编程技术网

Java Regex最佳实践

Java Regex最佳实践,java,regex,Java,Regex,我正在学习如何使用正则表达式: 我正在读一个文本文件,它被分成两个不同的部分,由 和。我需要知道每个部分是]还是},所以我不能就这么做 pattern.compile("<:==]:>|<:==}:>"); pattern.split(text) pattern.compile(“|”);模式.拆分(文本) 这样做: pattern.compile("<:=="); pattern.split(text) pattern.compile(“最好不要为此使用sp

我正在学习如何使用正则表达式:

我正在读一个文本文件,它被分成两个不同的部分,由
。我需要知道每个部分是
]
还是
}
,所以我不能就这么做

pattern.compile("<:==]:>|<:==}:>"); pattern.split(text)
pattern.compile(“|”);模式.拆分(文本)
这样做:

pattern.compile("<:=="); pattern.split(text)

pattern.compile(“最好不要为此使用
split()
。您可以改为进行匹配:

List<String> delimList = new ArrayList<String>();
List<String> sectionList = new ArrayList<String>();
Pattern regex = Pattern.compile(
    "(<:==[\\]}]:>)     # Match a delimiter, capture it in group 1.\n" +
    "(                  # Match and capture in group 2:\n" +
    " (?:               # the following group which matches...\n" +
    "  (?!<:==[\\]}]:>) # (unless we're at the start of another delimiter)\n" +
    "  .                # any character\n" +
    " )*                # any number of times.\n" +
    ")                  # End of group 2", 
    Pattern.COMMENTS | Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    delimList.add(regexMatcher.group(1));
    sectionList.add(regexMatcher.group(2));
} 
List delimList=new ArrayList();
List sectionList=新建ArrayList();
Pattern regex=Pattern.compile(
()#匹配分隔符,在组1中捕获它。\n+
“(#在组2中匹配并捕获:\n”+
(?:#匹配的以下组…\n”+
“(?!)#(除非我们位于另一个分隔符的开头)\n”+
“.#任何字符\n”+
“)*#任意次数。\n”+
“)#第2组末尾”,
Pattern.COMMENTS | Pattern.DOTALL);
Matcher regexMatcher=regex.Matcher(subjectString);
while(regexMatcher.find()){
delimList.add(regexMatcher.group(1));
sectionList.add(regexMatcher.group(2));
} 

我的初始解决方案(将分隔符包含在捕获组中)在Java中似乎不起作用(其他语言如Python也可以),所以我需要重新考虑这个问题。你能提供一个小的示例文件吗?我不太清楚这些节是如何被分隔的。它们是由成对的分隔符包围的,还是一个节在一个分隔符之后开始,然后以下一个分隔符结束?@TimPietzcker是的,我也有同样的认识。请参阅我的编辑,以了解fil如何被分隔的示例e的布局。它们不是成对的delimeter,每个delimeter的结尾由下一个的开始表示。此外,我应该注意,表示其他几种类型的标记。您到底希望输出什么?文本部分以及
]
}
?如果是,那么对于第一个/最后一个非delim的部分,您想要什么ited?你需要文本的一部分,还是只需要分隔符就足够了?看起来你已经完全解决了这个问题。我认为你所有问题的答案都是肯定的。有关详细信息,请查看此部分,尤其是和上的部分。至于你的最后一个问题,你能更具体一点吗?可能是以另一个问题的形式,因为评论是否定的我喜欢这个带有注释的示例,但请注意,静态正则表达式通常是静态编译(一次)并多次重用的
List<String> delimList = new ArrayList<String>();
List<String> sectionList = new ArrayList<String>();
Pattern regex = Pattern.compile(
    "(<:==[\\]}]:>)     # Match a delimiter, capture it in group 1.\n" +
    "(                  # Match and capture in group 2:\n" +
    " (?:               # the following group which matches...\n" +
    "  (?!<:==[\\]}]:>) # (unless we're at the start of another delimiter)\n" +
    "  .                # any character\n" +
    " )*                # any number of times.\n" +
    ")                  # End of group 2", 
    Pattern.COMMENTS | Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    delimList.add(regexMatcher.group(1));
    sectionList.add(regexMatcher.group(2));
}