C # regex and dollar sign

I created a regex to highlight certain assembly-style hexadecimal numbers that look like this:

$00 $1400 $FFFFFF 

Sometimes they also have #. So I created this regex as a start:

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

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

Why is this? I spent several hours trying to get this regular expression to work, but I just can't and have no idea why. Any help would be appreciated.

+4
source share
3 answers

\b matches an alphanumeric character and a non-alphanumeric character - it does not match between $ and # , space or other characters. You can completely abandon it:

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

If you do not want the pattern to match the alphanumeric character in front of it, you can add \b in front of it (so #$00 and a $00 will match, but a$00 will not). You can also be more picky and deny only certain characters:

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

See also: Word Boundaries

+4
source

The problem is the boundary of the word \b . It corresponds to the dollar sign. For example, this work will work @"(\$)[A-Fa-f\d]+"

+4
source

This will work if you remove \b at the beginning. Then he will become

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

\b corresponds to the boundary between a word character and a character without a word, and $ here is a character other than a word.

+2
source

All Articles