Mark Segal
Mark Segal

Reputation: 5550

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

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?

Upvotes: 0

Views: 240

Answers (1)

drf
drf

Reputation: 8699

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.

Upvotes: 3

Related Questions