Gijs
Gijs

Reputation: 434

Regex to match the first occurrence of a string

I have this string:

City - This is some text. This is some more - and continues here.

I would like to split the string at the first ' - ' to find 'city' (just a sample word, it can be other words as well). Plus to find the rest of the text after ' - '.

I constructed this expression:

(^[\D\W\S]*)( - )([\D\W\S]*)

But this finds the last occurrence of ' - ' instead of the first one.

How can I stop at the first occurrence ?

Upvotes: 22

Views: 59607

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

The simplest solution would be to explicitly forbid the dash to be part of the first group:

^([^-]*) - (.*)

Explanation:

^        # Start of string
([^-]*)  # Match any number of characters except dashes
\ - \    # Match a dash (surrounded by spaces)
(.*)     # Match anything that follows

However, this would fail if your string could contain a dash in the first group (just not surrounded by spaces). If that's the case, then you can make use of lazy quantifiers:

^(.*?) - (.*)

Explanation:

^        # Start of string
(.*?)    # Match any number of characters, as few as possible
\ - \    # Match a dash (surrounded by spaces)
(.*)     # Match anything that follows

Upvotes: 43

Related Questions