1

I want to match a number placed at the end of a string or maybe the middle.

The example would be:

"chapter some word 12 or IV" or "chapter some word 12 or IV some word"

the number what I want to extract is "12" or IV from the string.

I have tried to look around with ?:\w* or ?=\w* but it does not work.

My regex:

if (preg_match('/ch\w*\s*\K(?|(?=((?<=|\s)\d+(?:\.\d+)?))|(?=([ivx]+(?![a-z]))))/i', $string, $matches)){
    print_r($matches);
}

Am I missing something with the regex? Any pointers in the right direction would be appreciated.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
niznet
  • 107
  • 1
  • 11

1 Answers1

2

You may use

'~\bch\D*\K\d+~i'

See the regex demo

**To match any 1+ digits or Roman numbers from 1 to 10 (I to X) after the word use

'~\bch.*?\K\b(?:\d+|VI{0,3}|I(?:[XV]|I{0,2}))\b~i'

See this regex demo

Here, a set of alternatives is added to \d+: VI{0,3}|I(?:[XV]|I{0,2}).

Details

  • \b - starting word boundary, no letter, digit or _ should appear immediately to the left of the current position
  • ch - a literal substring
  • \D* - 0+ non-digit chars
  • \K - match reset operator
  • \d+ - 1+ digits
  • VI{0,3}|I(?:[XV]|I{0,2}) - V and 0, 1, 2 or 3 Is (that is, Roman 5, 6, 7, 8), or I that is followed with X or V (that is, 9 and 4) or with 0 (so, Roman 1 is matched), 1 or 2 Is (Roman 2 and 3).
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476