rwolst
rwolst

Reputation: 13662

Avoid capturing unwanted optional words in regex

I have the following example text

text = '. Double-Decker advances to 1st on throwing error. Chris Parmelee to 3rd.'

I want to capture the following information

['Double-Decker',  '1st']
['Chris Parmelee', '3rd']

I am trying the following regex

'\. ([A-Za-z\'\-\s]*) (?:advances)*to (1st|2nd|3rd)'

But it is matching

['Double-Decker advances', '1st']
['Chris Parmelee'        , '3rd']

How should I be dealing with the unwanted optional word advances?

Upvotes: 0

Views: 47

Answers (1)

logi-kal
logi-kal

Reputation: 7880

Use:

\. ([A-Za-z\'\-\s]*?) (?:advances )*to (1st|2nd|3rd)

In your regex there are two small errors:

  1. * operator is greedy: you should use [A-Za-z\'\-\s]*?
  2. advances does not have a space after, so it won't ever match "advances to".

Upvotes: 2

Related Questions