问题
I'm new to regex and I want to highlight hexadecimal numbers in Assembly style. Like this:
$00
$FF
$1234
($00)
($00,x)
and even hexadecimal numbers that begin with #.
So far I wrote "$[A-Fa-f0-9]+" to see if it highlights numbers beginning with $ but it doesn't. Why? And can someone help me with what I'm doing? Thanks.
回答1:
Put a back slash before $ and your regex will work like so
\$[A-Fa-f0-9]+
$ is a valid regex character that matches with end of string. So if your pattern contains dollar then you need to escape it. See regex reference for details
回答2:
This should cover all those cases, including the cases in which you get a # instead of a $
public Regex MyRegex = new Regex(
"^(\\()?[\\$#][0-9a-fA-F]+(,x)?(?(1)\\))[\\s]*$",
RegexOptions.Singleline
| RegexOptions.Compiled
);
The unescaped sequence for the single line: ^(\()?[\$#][0-9a-fA-F]+(,x)?(?(1)\))[\s]*$
That should validate on a per-line match.
By the way, I made this regex pretty quickly using Expresso
来源:https://stackoverflow.com/questions/7857275/c-sharp-regex-for-assembly-style-hex-numbers