-3

I have some records as such, in a file:

A 20 year old programmer
A 52 year old politician
A 12 year old practitioner
Many many more of these...

I want to match only lines that contain a number less than 20. I have tried:

^[0-20]{2}$

But it works for only numbers 0-2. How should I construct a regular expression to match numbers < 20? For instance, it should match:

A 12 year old practitioner

But not

A 20 year old programmer
A 52 year old politician
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79

2 Answers2

3

You may use

\b1?[0-9]\b

See the regex demo

Details

  • \b - a word boundary
  • (?:1?[0-9]) - an optional 1 and any digit after it
  • \b - a word boundary

Word boundary variations

To match anywhere in a string, even if glued to a word:

(?<!\d)1?[0-9](?!\d)

To only match in between whitespaces:

(?<!\S)1?[0-9](?!\S)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    That "glued to a word" one is exactly what I want, thanks so much! I'll accept this when the grace period ends. – Jack Bashford Nov 15 '18 at 08:05
  • @JackBashford Great. Note that in case you want to also match `00` you need to replace `1?` with `[01]?` – Wiktor Stribiżew Nov 15 '18 at 08:06
  • Ahh, because that would "optionally match a `0` or a `1`"? – Jack Bashford Nov 15 '18 at 08:07
  • @JackBashford `[01]` is a [**character class**](https://www.regular-expressions.info/charclass.html) matching any single char defined inside the class, and `?` is a greedy quantifier matching the quantified pattern 1 or 0 times. – Wiktor Stribiżew Nov 15 '18 at 08:09
  • 1
    @JackBashford Also, see [Why doesn't \[01-12\] range work as expected?](https://stackoverflow.com/questions/3148240/why-doesnt-01-12-range-work-as-expected) – Wiktor Stribiżew Nov 15 '18 at 08:10
1

Using regex to match digit ranges is usually a bit clumsy, but here, you can do it pretty simply with:

\b1?\d\b

https://regex101.com/r/YCWmNo/2

In plain language: an optional one, followed by a digit. So, any standalone digit is allowed, but a two-digit number needs its first digit to be a 1.

If you want to permit leading zeros, change to \b[01]?\d\b.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320