As Go's regular expressions are based on RE2, I always use the latter's 
documentation 
page <https://github.com/google/re2/wiki/Syntax> to check what is and isn't 
allowed.

Note though that: \C, which RE2 normally allows, isn't allowed in Go.

Alan

On Tuesday, January 7, 2020 at 6:21:58 PM UTC, Tom Payne wrote:
>
> Hi,
>
> tl;dr How should I use named Unicode character classes in regexps?
>
> I'm trying to write a regular expression that matches Go identifiers 
> <https://golang.org/ref/spec#Identifiers>, which start with a Unicode 
> letter or underscore followed by zero or more Unicode letters, decimal 
> digits, and/or underscores.
>
> Based on the regexp syntax <https://godoc.org/regexp/syntax>, and the 
> variables 
> in the unicode package <https://golang.org/pkg/unicode/#pkg-variables> which 
> mention the classes "Letter" and "Number, decimal digit", I was expecting 
> to write something like:
>
>   identiferRegexp := 
> regexp.MustCompile(`\A[[\p{Letter}]_][[\p{Letter}][\p{Number, decimal 
> digit}]_]*\z`)
>
> However, this pattern does not compile, giving the error:
>
>   regexp: Compile(`\A[[\p{Letter}]_][[\p{Letter}][\p{Number, decimal 
> digit}]_]*\z`): error parsing regexp: invalid character class range: 
> `\p{Letter}`
>
> Using the short name for character classes (L for Letter, Nd for Number, 
> decimal digit) does work however:
>
>   identiferRegexp := regexp.MustCompile(`\A[\pL_][\pL\p{Nd}_]*\z`)
>
> You can play with these regexps on play.golang.org 
> <https://play.golang.org/p/WITTbqvom9F>.
>
> Is this simply an oversight that Unicode character classes like "Letter" 
> and "Number, decimal digit" are not available for use in regexps, or should 
> I be using them differently?
>
> Many thanks,
> Tom
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e6307afc-889c-46de-87b0-6f9a06620421%40googlegroups.com.

Reply via email to