当前位置: 动力学知识库 > 问答 > 编程问答 >

C# regex for assembly style hex numbers

问题描述:

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.

网友答案:

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: