查找字符串中以点分隔的所有子字符串?

时间:2014-11-02 06:43:13

标签: java regex string

我有一个大字符串,我需要找到所有的存在 来自模式的子字符串:

[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子字符串

任何帮助将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:3)

更明确:

[^\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

&#13;
&#13;
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)
&#13;
&#13;
&#13;