Regular expression to search for all cell addresses in a row

I have a line that may contain a cell address that looks like this:
A1, B34, Z728 - only uppercase letters and
AA3, ABA92, ZABC83 - there may be several letters up to an integer.
A typical source line looks like this:
= 3 + 7 * A1-B3 * AB28
I need to collect all the cells in a row: A1, B3, AB28
I tried using the Regex.Matches method with the following regex: @ "[A..Z] +? [1..9] +?", But it does not work. Can someone help me write a regex?

+4
source share
1 answer

There are three errors in your regex:

  • Character class ranges are specified using - , not ..
  • Cell addresses can contain the number 0, not the first digit.
  • Greedy default is what you want. Your lazy coincidence will lose numbers from the end.

Try the following:

 "[AZ]+[1-9][0-9]*" 

Example:

  string input = "3+7*A1-B3*AB28"; foreach (Match match in Regex.Matches(input, "[AZ]+[1-9][0-9]*")) Console.WriteLine(match.Value); 

Conclusion:

 A1 B3 AB28 
+5
source

Source: https://habr.com/ru/post/1305619/


All Articles