Java 查找自定义标记之间是否存在文本
在下面的代码中,我可以找到Java 查找自定义标记之间是否存在文本,java,regex,Java,Regex,在下面的代码中,我可以找到hello there之间的任何文本,例如getp | a | s和hello there,没有任何问题 Pattern mftA_REGEX = Pattern.compile("<(.+?)>(.+?)</>"); Matcher matcher = mftA_REGEX.matcher("<p|a|s>hello there</>"); if (matcher.find()) { Lo
hello there
之间的任何文本,例如getp | a | s
和hello there
,没有任何问题
Pattern mftA_REGEX = Pattern.compile("<(.+?)>(.+?)</>");
Matcher matcher = mftA_REGEX.matcher("<p|a|s>hello there</>");
if (matcher.find()) {
Log.e("tag ",matcher.group(1));
Log.e("text ",matcher.group(2));
}
Pattern-mftA_-REGEX=Pattern.compile((.+?));
Matcher Matcher=mftA_REGEX.Matcher(“你好”);
if(matcher.find()){
Log.e(“tag”,matcher.group(1));
Log.e(“text”,matcher.group(2));
}
现在,当我没有
p | a | s
像hello那样时,matcher找不到。在我的字符串p | a | s
是可选的,如何更改Pattern.compile(.+?)”
若要解决此问题?可选?
组上的应在组外,即之后)
:
或者,如果要匹配空字符串,则:
<(.*)>
请参见关于的工作示例,似乎(.*)
优于(.+)?
,因为可以正确找到(.*)
,您的建议是什么?我明白了,这取决于您是不想匹配还是匹配空字符串。@westom我想匹配空字符串OK使用(.*)
。
<(.*)>