Faye D.
Faye D.

Reputation: 732

Regex pattern to match numbers but also mixed numbers

I'd like to be able to identify correctly integers but also mixed numbers (integers + + a fraction).

So I'd like to match all of the below

48
34 1/2
34
46 2/3
42 1/2''
38 2/3"
24''
26"

What's the correct pattern to do so? I tried /\d+ \d+\/\d+/ but that matches only the mixed numbers, leaving plain integers outside...

Thanks in advance for your help!

Edit: Ideally, I'd like it to identify two single quotes, or a double quote at the end also... I updated the list!

Upvotes: 1

Views: 152

Answers (1)

MonkeyZeus
MonkeyZeus

Reputation: 20737

This would work for you:

\d+(?: \d+\/\d+)?(?:''|")?
  • \d+ - capture one or more digits
  • (?: \d+\/\d+)? - optionally capture a fraction preceded by a space
  • (?:''|")? - optionally capture either two single quotes or a double quote

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

Upvotes: 1

Related Questions