Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# regex and dollar sign

Tags:

c#

regex

hex

I have created a regex for highlighting certain assembly-styled hex numbers which are like this:

$00  
$1400  
$FFFFFF

Sometimes they are preceeded with a # as well. So I created this regex as a start:

@"\b(\$)[A-Fa-f\d]+\b"

When I tried it out, it didn't seem to match anything. However, if I replace the \$ with 0x, it works fine and returns matches for C# style hex-numbers like 0x0F, 0xFF, etc.

Why is this? I have spent a few hours trying to make this regex work but I just can't and have no idea why. Any help would be appreciated.

like image 578
rayanisran Avatar asked Oct 23 '11 08:10

rayanisran


1 Answers

\b matches between an alphanumeric character and a non-alphanumeric character - it does not match between $ and #, space, or other characters. You may want to drop it entirely:

@"(\$)[A-Fa-f\d]+\b"

If you don't want the pattern to match with an alphanumeric character before it, you can add \B before it (so #$00 and a $00 would match, but a$00 would not). You can also be more picky, and disallow only certain characters:

@"(?<=[\w$])(\$)[A-Fa-f\d]+\b"

See also: Word Boundaries

like image 200
Kobi Avatar answered Nov 15 '22 00:11

Kobi