Go (RE2)

Hexadecimal Number Literal in GO

Match hexadecimal number literals like `0xFF`, `0x1A2B`, or `0XdeadBeef`.

Try it in the GO tester →

Pattern

regexGO
\b0[xX][0-9a-fA-F]+\b   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\b0[xX][0-9a-fA-F]+\b`)
	input := `Mask = 0xFF; magic = 0xDEADBEEF;`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

\b is a word boundary so we don't match inside identifiers like `var0x10`. 0[xX] requires the literal prefix (case-insensitive on the x). [0-9a-fA-F]+ matches one or more hex digits in either case. Trailing \b avoids matching into adjacent letters.

Examples

Input

Mask = 0xFF; magic = 0xDEADBEEF;

Matches

  • 0xFF
  • 0xDEADBEEF

Input

Color #ff0000 vs literal 0xff0000

Matches

  • 0xff0000

Input

no hex here

No match

Same pattern, other engines

← Back to Hexadecimal Number Literal overview (all engines)