priyanka.sarkar
priyanka.sarkar

Reputation: 26518

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

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?

Upvotes: 1

Views: 473

Answers (1)

Matt Ellen
Matt Ellen

Reputation: 11612

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

Upvotes: 5

Related Questions