1

I am trying to get value less than 230001 in list and two number after space 20 or 10 and Last character is X. I tried:

[0-2][0-3][0-9][0-9][0-9][0-9] (20|10) X
-z001 230001 20 X
-z011  26321 10 *
-z002 230000 10 X
-z003 134242 12 *
-z004 199999 10 X
-z005 299999 20 X

My expected result is

-z002 230000 10 X
-z004 199999 10 X
JvdV
  • 70,606
  • 8
  • 39
  • 70
  • 2
    You should do this check using an inequality comparison from your code if possible. – Tim Biegeleisen Aug 25 '22 at 09:24
  • 1
    Regext are not fit for this kind of problem – Carlo Moretti Aug 25 '22 at 09:26
  • 1
    What's your context? If you're trying to grep files, you could get the 20 or 10 and the X, then sort the rest or do some AWK or similar... Or are you doing this in code somewhere? – doctorlove Aug 25 '22 at 09:28
  • Why is `-z005 299999 20 X` an expected result? To me `299999` is higher than `230000`. Have a look at [this](https://regex101.com/r/gTAbqU/1) – JvdV Aug 25 '22 at 09:36
  • I mean my expect result is -z002 230000 10 X -z004 199999 10 X sorry but i dont know how to edit this post :D – Iwan Kurniawan Aug 25 '22 at 09:42
  • 1
    you just have to list all the possibilities - https://regex101.com/r/mu3yYM/1 - but as other people have said, a simple numerical comparison would be better – jhnc Aug 25 '22 at 10:31

1 Answers1

2

You can use

.* ([0-9]|[1-9][0-9]{1,4}|1[0-9]{5}|2[0-2][0-9]{4}|230000) [12]0 X.*

See the regex demo.

The numeric range regex is generated with this numeric range regex generator.

Details:

  • .* - any zero or more chars as many as possible
  • - space
  • ([0-9]|[1-9][0-9]{1,4}|1[0-9]{5}|2[0-2][0-9]{4}|230000) - 0 to 23000 number regex
  • - space
  • [12]0 - 1 or 2 and then 0
  • X - space, X
  • .* - any zero or more chars as many as possible.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563