Home > Back-end >  Regex not select word with character at the end
Regex not select word with character at the end

Time:04-02

I have a simple question. I need a regular expression to match a hexdecimal number without colon at the end. For example:

0x85af6b9d: 0x00256f8a ;some more interesting code
// dont match 0x85af6b9d: at all, but match 0x00256f8a

My expression for hexdecimal number is 0[xX][0-9A-Fa-f]{1,8}

Version with (?!:) is not possible, because it will just match 0x85af6b9 (because of the {1,8} token) Using a $ also isn't possible - there can be more numbers than one

Thanks!

CodePudding user response:

Here is one way to do so:

0[xX][0-9A-Fa-f]{1,8}(?![0-9A-Fa-f:])

See the online demo.


We use a negative lookahead to match all hexadecimal numbers without : at the end. Because of {1,8}, it is also necessary to ensure that the entire hexadecimal number is correctly matched. We therefore reuse the character set ([0-9A-Fa-f]) to ensure that the number does not continue.

  • Related