0

Sample file contents:

create 664 root utmp

I would like to match any number that is NOT this:

[0-6][0-4][0]

So it would match 664 and 641, but not 440 and 640.

My full regex is this:

^([\s]`*`?)create[\s]+?([0]?[0-6][0-6][0-6])[\s]+?(\w+?)[\s]+?(\w+?)[\s]`*`?$

But I would like to basically say:

  • match on not this:

    ([0]?[0-6][0-6][0-6])
    
Dennis
  • 3
  • 2
  • Depending on the tool or language, you can use a negative lookahead `create(?!\s+[0-6][0-4]0)\s+(\d{3})\b` https://regex101.com/r/jalFbw/1 – The fourth bird Mar 12 '23 at 10:04
  • Dupe: https://stackoverflow.com/questions/977251/regular-expressions-and-negating-a-whole-character-group – Nasser Kessas Mar 12 '23 at 10:09
  • thanks "bird" and Nasser - helpful comments. Also helpful: https://docs.racket-lang.org/guide/Looking_Ahead_and_Behind.html – Dennis Mar 15 '23 at 19:39

2 Answers2

1

I looked at: How can I "inverse match" with regex?

and found: (?![0-6][0-4][0])[0-9]{3} see: https://regex101.com/r/D3JRfu/1

As far as I can see this works.

Luuk
  • 12,245
  • 5
  • 22
  • 33
0

Using the regex in your sample [0]?[0-6][0-6][0-6],
it can be done with a couple of negative assertions.
There has to be boundary assertions to force a constraint on the number of
digits as well as values.

(?<!\d)(?![0]?[0-6][0-6][0-6](?!\d))\d+  

The first negative look behind can be replaced with a \s when its put in the larger regex.

 (?<! \d )
 (?!
    
    [0]? [0-6] [0-6] [0-6] 
    (?! \d )
    
 )
 \d+ 
sln
  • 2,071
  • 1
  • 3
  • 11