开发者

What regular expression can I use to match a cell reference?

开发者 https://www.devze.com 2023-01-14 19:56 出处:网络
For one of my projects I want to use a regular expression to match a string like \"REF:Sheet1!$C$6\".

For one of my projects I want to use a regular expression to match a string like "REF:Sheet1!$C$6".

So far I have done

public static private bool IsCellReference()
        {

           string CELL_REFERENCE_PATTERN = @"REF:Sheet[1-9]!$[A-Z]$[0-9]";

         开发者_运维技巧   Regex r = new Regex(CELL_REFERENCE_PATTERN);
            Match m = r.Match("REF:Sheet1!$C$6");
                if (m.Success) return true;
                else return false;
        }

but it is not working. It is returning false.

Where am I wrong?


You need to escape your $ signs.

REF:Sheet[1-9]!\$[A-Z]\$[0-9]

See Regular Expression Language Elements for more information

Also, this page is good for testing your regexes: A better .NET Regular Expression Tester

0

精彩评论

暂无评论...
验证码 换一张
取 消