我有同样的问题这个链接 https://stackoverflow.com/questions/18751486/matching-one-string-multiple-times-using-regex-in-java
但有多种模式。我的正则表达式是这样的:
Pattern word = Pattern.compile("([\w]+ [\d]+)|([\d]+ suite)|([\w]+ road)");
如果我的示例文本是,
XYZ路123套房
我的愿望输出是,
XYZ路123号
123套房
但我越来越
XYZ路123号
only.
提前致谢!
您可以尝试以下使用正向先行断言的正则表达式。
(?=(\b\w+ Road \d+\b)|(\b\d+ suite\b))
DEMO https://regex101.com/r/dU7oN5/17
String s = "XYZ Road 123 Suite";
Matcher m = Pattern.compile("(?i)(?=(\\b\\w+ Road \\d+\\b)|(\\b\\d+ suite))").matcher(s);
while(m.find())
{
if(m.group(1) != null) System.out.println(m.group(1));
if(m.group(2) != null) System.out.println(m.group(2));
}
Output:
XYZ Road 123
123 Suite
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)