Go (RE2)

HTML Entity in GO

Match HTML entities in named (`&`), numeric (`{`), or hex (`💩`) form.

Try it in the GO tester →

Pattern

regexGO
&(?:[a-zA-Z][a-zA-Z0-9]+|#\d+|#x[0-9a-fA-F]+);   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`&(?:[a-zA-Z][a-zA-Z0-9]+|#\d+|#x[0-9a-fA-F]+);`)
	input := `Tom & Jerry <3`
	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

The leading `&` and trailing `;` bracket the entity. The middle alternation matches: a named entity ([a-zA-Z][a-zA-Z0-9]+ — letters then alphanumerics, like `amp`, `lt`, `nbsp`); a decimal entity (#\d+, like `#160`); or a hex entity (#x[0-9a-fA-F]+, like `#xA0` or `#x1F600` for emoji).

Examples

Input

Tom & Jerry <3

Matches

  • &
  • <

Input

Numeric:   Hex: 😀

Matches

  •  
  • 😀

Input

no entities here

No match

Same pattern, other engines

← Back to HTML Entity overview (all engines)