Search code examples
regexregex-negation

Regex - to match number NOT in a sequence of character classes


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])
    

Solution

  • 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.