Regex only capture first match

Wancak Terakhir picture Wancak Terakhir · Sep 1, 2014 · Viewed 45.3k times · Source

I want to parse a string using regex, example of the string is

Lot: He said: Thou shalt not pass!

I want to capture Lot as a group, and He said: Thou shalt not pass!. However, when I used my (.+): (.+) pattern, it returns

Lot: He said: and Thou shalt not pass!

Is it possible to capture He said: Thou shalt not pass using regex?

Answer

sp00m picture sp00m · Sep 1, 2014

You need a non-greedy (or lazy) cardinality for the first group: (.+?): (.+).

More details on http://www.regular-expressions.info/repeat.html, chapter "Laziness Instead of Greediness".