Paul Peelen
Paul Peelen

Reputation: 10329

Regex which should contain phrase and a matching group in any order

I'm trying to check a string for the phrase "Alla dagar" either presided or followed by digit(possible space?)-(possible space?)digit and it may or may not contain a line break in between.

Examples:

### Item 1 - Should match
8-17
Alla dagar

### Item 2 - Should match
Alla dagar
08-21

### Item 3 - Should not match
8-17

### Item 4 - Should match
7 - 9 Alla dagar

### Item 5 - Should match
Alla dagar 17-23

I came up with two regex which neither I get to work. The first is this one:

^(?=.*s|Alla dagar)(?=(?<from>\d+) ?- ?(?<to>\d+)).*$

The other regex I wrote didn't work either, because it's not exclusive. It matches also if "alla dagar" doesn't exist because these are optional.

^((?:s|Alla dagar))?\n*?((?<from>\d+) ?- ?(?<to>\d+))\n*?((?:s|Alla dagar))?\n?$

My question, how can I make a match using ((?<from>\d+) ?- ?(?<to>\d+)) and require the phrase Alla dagar to exist in the string?

Upvotes: 1

Views: 40

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You can use

(?<from>\d+)\s*-\s*(?<to>\d+)\s*Alla dagar|Alla dagar\s*(?<from>\d+)\s*-\s*(?<to>\d+)

See the regex demo. Make sure you use the PCRE_INFO_JCHANGED flag ((?J) at the start of the regex can be used in case you want to incorporate it into the regex itself) to enable multiple named capturing groups inside a single expression.

Details:

  • (?<from>\d+) - Group "from": one or more digits
  • \s*-\s* - a hyphen enclosed with zero or more whitespaces
  • (?<to>\d+) - Group "to": one or more digits
  • \s* - zero or more whitespaces
  • Alla dagar - a fixed string
  • | - or
  • Alla dagar\s*(?<from>\d+)\s*-\s*(?<to>\d+) - a fixed string, zero or more whitespaces, Group "from": one or more digits, a hyphen enclosed with zero or more whitespaces, Group "to": one or more digits.

Upvotes: 2

Related Questions