将Java中的正则表达式转换为golang。 纠正我

Java Regular expression:

str.replaceFirst("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5")

What I have tried in Go:

re:=regexp.MustCompile("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)")

repStr := "$1-$2-$3-$4-$5"

str1:=re.ReplaceAllString(someString,repStr)

It says that the XDigit is an unknown character category

In Java regex, the \p{XDigit} is just a shorthand for [0-9a-fA-F]:

POSIX character classes (US-ASCII only)
\p{Lower}               A lower-case alphabetic character: [a-z]
\p{Upper}               An upper-case alphabetic character:[A-Z]
\p{ASCII}               All ASCII:[\x00-\x7F]
\p{Alpha}               An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit}               A decimal digit: [0-9]
\p{Alnum}               An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct}               Punctuation: One of !"#$%&'()*+,-./:;?@[\]^_`{|}~
\p{Graph}               A visible character: [\p{Alnum}\p{Punct}]
\p{Print}               A printable character: [\p{Graph}\x20]
\p{Blank}               A space or a tab: [ \t]
\p{Cntrl}               A control character: [\x00-\x1F\x7F]
\p{XDigit}              A hexadecimal digit: [0-9a-fA-F]
\p{Space}               A whitespace character: [ \t
\x0B\f]

The same [0-9a-fA-F] character class can be expressed with a [[:xdigit:]] in Go regex.

You may write the Go pattern like

re:=regexp.MustCompile("([[:xdigit:]]{8})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]+)")

Or,

re:=regexp.MustCompile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)")

You may see the online Go regex demo here and here is a Go demo:

package main

import (
    "fmt"
    "regexp"
)

const sample = `0a1f0a1f0a1f0a1f0a1f0a1f0a1f 0a1f0a1f0a1f0a1f0a1f0a1f0a1f`

func main() {
    re := regexp.MustCompile(`([[:xdigit:]]{8})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]+)`)
    repl := `$1-$2-$3-$4-$5`
    fmt.Println(re.ReplaceAllString(sample, repl))
}

Result: 0a1f0a1f-0a1f-0a1f-0a1f-0a1f0a1f 0a1f0a1f-0a1f-0a1f-0a1f-0a1f0a1f.

Note that if you need to replace only the first occurrence (as in Java, you are using String#replaceFirst), you will probably want to match the whole string with .*? before the pattern you need to replace, and then .* after it to match the rest of the string, capture them and adjust the replacement string to contain the placeholders for these two additional parts of the string:

re:=regexp.MustCompile("^(.*?)([[:xdigit:]]{8})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]{4})([[:xdigit:]]+)(.*)$")
repStr := "$1$2-$3-$4-$5-$6$7"

See another regex demo.