我有一个很大的字符串,我需要在其中找到所有的存在 模式中的子字符串:
[Word][Dot][Word][Dot]...[Word]
我遇到了一些麻烦,尤其是重复的 [Word][Dot][Word]
模式
这是字符串:
"a.a b..b c.c.c d. .e ff g...g hh.h i.i..i"
这是我当前的模式:
\S+[.{1}]\S+
Matcher.find()
返回这些子字符串:
[0,3] a.a
[4,8] b..b - shouldn't match
[9,14] c.c.c
[24,29] g...g - shouldn't match
[30,34] hh.h
[35,41] i.i..i - shouldn't match
我无法让它忽略 b、g、i 子字符串
如有任何帮助,我们将不胜感激。 谢谢!
最佳答案
更明确:
[^\s.]+(?:\.[^\s.]+)+(?!\S)
说明:
[^\s.]+ # Match one or more characters exceopt dot or whitespace
(?: # Start a non-capturing group
\. # Match a dot
[^\s.]+ # Match one or more characters exceopt dot or whitespace
)+ # Repeat as often as necessary
(?!\S) # Make sure we don't stop before a non-whitespace character
测试一下 live on regex101.com .
subject = "a.a b..b c.c.c d. .e ff g...g hh.h i.i..i";
result = subject.match(/[^\s.]+(?:\.[^\s.]+)+(?!\S)/g);
document.write(result)
关于java - 查找字符串中所有用点分隔的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26696750/