10

Is there a regular expression pattern to match an Excel sheet name and address,...

 3 years ago
source link: https://www.codesd.com/item/is-there-a-regular-expression-pattern-to-match-an-excel-sheet-name-and-address-that-is-a-cell-reference.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Is there a regular expression pattern to match an Excel sheet name and address, that is, a cell reference

advertisements

Some valid sheeet names are as under among the other various names

t'#3cfbsdjfdsjb''''''''^^^^^^&&'!$C$8:$N$23   

1'!$E$13:$H$19    

!'!$B$7:$E$16    

@@@@@@@'!$B$7:$K$21

!$'!$B$12:$M$17

Iam using

string pattern =@"[^<]+?\!\$[A-Z]+\$[0-9]+"

Regex r = new Regex(Constants.CELL_REFERENCE_PATTERN);
            Match m = r.Match(selectedVal);
            if (m.Success) return true;
            else return false;

but it is failing for

"t'#3cfbsdjfdsjb''''''''^^^^^^&&'!$C$8:$N$23,p,mv" or "sheeet1!$A1."

Thanks in advance


Ignoring the C# factor, these are regular expressions that I have found to work.

Cell Reference with sheet optional (<sheet>!<cell>)

"(('[^/\\?*\[\]]{1,31}'|[A-Za-z0-9_]{1,31})!)?((\$?[A-Za-z]{1,3})(\$?[0-9]{1,6}))"

Notes:

  • This regex does not validate Cell values, ex. ZZZ999999 is valid through the regex, but it is not a valid location inside of Excel.
  • This is for Excel 2007+. For pre-2007, change {1,3} to {1,2} and {1,6} to {1,5}. This is because the range of cells went from IV65536 (pre-2007) to XFD1048576 (2007+). But of course, there is no validation and technically it could be [A-Za-z]+ and [0-9]+.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK