behzad
behzad

Reputation: 196

match.regex syntax with digit character and a #

i have a string with this format :

111111#1

the number of digit character is 5 or 6 and after that i set a '#' and also set a digit charcter.

i use Regex.IsMatch like this :

if (Regex.IsMatch(string, @"^d{6}#\d{1}"))
{...}

but it cant handle my string what is my mistake?

Upvotes: 0

Views: 33

Answers (2)

BionicCode
BionicCode

Reputation: 29018

This single line Regex will capture two groups: the leading five to six digits and the '#' followed by a single digit:

(\d{5,6})(#\d{1})

Example:

string pattern = @"(\d{5,6})(#\d{1})";
string input = "111111#1";

MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
  var firstGroupValue = match.Groups[1]; // "111111"
  var secondGroupValue = match.Groups[2]; // "#1"
}      

Upvotes: 1

Gavin Ward
Gavin Ward

Reputation: 1022

You're missing the backslash on the first d so it's not matching against digits:

Regex.IsMatch("111111#1", @"^\d{6}#\d{1}")

Upvotes: 2

Related Questions