Use the ':' (0x3A) character in a Regex sentence?

Issue

No, this is not a replicate of The ':' character, hexadecimal value 0x3A, cannot be included in a name

I have a Regex for my syntax highlighter for my scripting language:

`@"\bClass+(?<range>\w+?)\b"` 

which basically marks Class Name

(with the engine I got online)

I’m no master in Regex but for some reason the : character that uses my language to create labels – doesn’t work.

I tried

 @"\b:+(?<range>\w+?)\b"`, `@"\b\:+(?<range>\w+?)\b"`<RB> `@"\b(\x3A)+(?<range>\w+?)\b"

And it refuses to work!

Any ideas?

Solution

I suspect the issue in your case is not the : itself, but the \b before it. \b marks the boundary between a word character and nonword character, but while Class is comprised of word characters, : is a nonword character. So \b is behaving differently for : than it would for Class, so:

`\bClass` matches " Class Name"
`\b:` does not match " : Name"

If you use your original expression but replace the first \b with (?<!\w), it may identify the : properly.

Answered By – drf

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.