itoor
itoor

Reputation: 23

Find all sub strings separated by dot in a string?

I have a large string in which I need to find all the presences of a sub-string from the pattern:

[Word][Dot][Word][Dot]...[Word]

I'm having some troubles, especially with the repeatedly [Word][Dot][Word] pattern

Here's the string:

"a.a b..b c.c.c d. .e ff g...g hh.h i.i..i"

and here's my current pattern:

\S+[.{1}]\S+

The Matcher.find() return those sub strings:

[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  

I can't make it ignore the b,g,i sub strings

Any help would be appreciated. Thanks!

Upvotes: 2

Views: 214

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Be more explicit:

[^\s.]+(?:\.[^\s.]+)+(?!\S)

Explanation:

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

Test it 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)

Upvotes: 3

Related Questions