clarkk
clarkk

Reputation: 27719

regex: OR operator between multiple options

https://regex101.com/r/VY5LIG/1

pattern

(?:faktura|kreditnota|kvittering)\b ?((?:\p{L}+\d+|(?:[\d ]|[^\p{L}])*))

subject

faktura/kvittering 6856895

Why does this regex not match kvittering in this case?

The group needs to match the number

Upvotes: 1

Views: 4219

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627101

The match you get is due to the fact that (?:faktura|kreditnota|kvittering) matched the first faktura and then [^\p{L}] matched the slash after the word.

If you grabbed all matches, you could get the last one, and access [Group 1 value].1

Another way to get the last match would be to add .* at the start of the pattern and add s modifier to make sure . matches any char including a newline, and you would be able to get the match with a preg_match and again, grab Group 1 value. See this regex demo.

However, from the description it seems you need to match a word from your first alternation group and then a number or some code that seems to consist of alphanumeric/word chars. Thus, I suggest using:

'~\b(?:faktura|kreditnota|kvittering)\s+\K\w+~'

See this regex demo.

Pattern details

  • \b - a leading word boundary
  • (?:faktura|kreditnota|kvittering) - any of the three words
  • \s+ - 1 or more whitespaces
  • \K - a match reset operator that omits the text matched so far from the match memory buffer
  • \w+ - 1+ word chars (you may use [^\W_] to match just alphanumeric ones).

Upvotes: 2

Related Questions