richwol
richwol

Reputation: 1165

Regular expression split string

I'm looking to split a string (and get all the matches) using a regular expression.

I have the following string:

ppc.goo.gen.heat..jan-17.logo

It should return everything between each .

[0] ppc
[1] goo
[2] gen
[3] heat
[4] 
[5] jan-17
[6] logo

So far I have this:

([a-zA-Z0-9-]*)\.

Which matches 0 to 5, but it'll miss off match 6 as it doesn't have a . at the end. I tried using lookahead but I couldn't get it working. Any tips?

Upvotes: 0

Views: 88

Answers (1)

Whothehellisthat
Whothehellisthat

Reputation: 2152

(?:^|\.)([^\.]*)(?=\.|$)

Group 1 will be the entry.

  • (?:^|\.) Match start of string, or .
  • ([^\.]*) Match 0 or more non-. characters
  • (?=\.|$) Match . or end of string

Upvotes: 1

Related Questions