Find text starts with "Column" and ends with any number (e.g. "100") and nothing between the two, using C#

StackOverflow https://stackoverflow.com/questions/21305024

Question

I want to find texts (using C#), in a string, that starts with the word "Column" and ends any number (for example "100").

In short, I want to find:

Column1
Column100
Column1000

But not to find:

Column_1
_Column1
Column1$

I can't find a way to do it, using regular expressions.

Was it helpful?

Solution

This is practically as easy as regular expressions get.

^Column\d+$

OTHER TIPS

Another way without Regex :

public string getColumnWithNum(string source)
{
     string tmp = source;
     if (tmp.StartsWith("Column"))
     {
          tmp.Replace("Column", "");
          UInt32 num
          if (UInt32.TryParse(tmp, out num)
          {
               return source; // matched
          }
     }
     return ""; // not matched
}

this should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top