简体   繁体   中英

How can I validate an Excel cell reference that includes a sheet name?

I want to validate a cell reference in an Excel sheet formula.

Valid strings:

  1. Sheet1!A1
  2. 'Sheet!!'!A1

I am using following expression but it is not working.

"^='?[^[]/\*]*'?!+[A-Z]+[0-9]+$"
Regex re = new Regex(@"^=('?[^']+'?|[^!]+)![A-Z]+[0-9]+$");
bool isOk = re.IsMatch("=Sheet1!A1");

Change the [^!] to whatever characters you do not want in the sheet's name.

It will match anything with the format ='<anything>'!XN or =<anything>!XN where X is at least one uppercase letter and N is at least one digit.

Edit : I am not sure if OP wants the = character at the start or not (the original post had it). If you don't, remove the = character from the start of the regular expression.

Oh some other problem.

Its working now

^='?[^/\[]*]*'?!{1}[A-Z]+[0-9]+$

这应该可以工作:=?(\\ s | \\ w)+![A-Za-z] + \\ d +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM