doublnt
doublnt

Reputation: 571

Can I get each regex group match count in C#?

Here is the text:

There are many kids in the playground. It's about 1 23  45 5;中文等等

Here is the Regex Pattern I use ([0-9])|([a-zA-Z])|([\u4e00-\u9fa5])" I want to get the three match group count:pattern1 [0-9] the number is 6; The [\u4e00-\u9fa5] count is 4,Is there possible to calculate that.

I have tried

var characterMatch = characterPattern.Matches(content)

but I only get the all match count is 49. So, is there possible to get the different part count?

What I expected to get is that the match the [0-9] count, match the [a-zA-Z] count

Upvotes: 1

Views: 548

Answers (2)

Marc
Marc

Reputation: 1467

You can use this expression:

(?<digit>[0-9])|(?<letter>[a-zA-Z])|(?<ucode>[\u4e00-\u9fa5])

in that code:

string strRegex = @"(?<digit>[0-9])|(?<letter>[a-zA-Z])|(?<ucode>[\u4e00-\u9fa5])";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string strTargetString = @"There are many kids in the playground. It's about 1 23  45 5;????";

int digits = 0;
int letters = 0;
int ucode = 0;
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
    digits += (!string.IsNullOrEmpty(myMatch.Groups["digit"].Value) ? 1 : 0);
    letters += (!string.IsNullOrEmpty(myMatch.Groups["letter"].Value) ? 1 : 0);
    ucode += (!string.IsNullOrEmpty(myMatch.Groups["ucode"].Value) ? 1 : 0);
}

In a single iteration counts all matches.

Note: To test regular expressions on c# online I use http://regexhero.net/tester/ (Silverlight only in IE... O_o)

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

You need to count all Group 1, 2 and 3 capture group values that are not empty:

var s = "There are many kids in the playground. It's about 1 23  45 5;中文等等";
var pattern = @"([0-9])|([a-zA-Z])|([\u4e00-\u9fa5])";
var ms = Regex.Matches(s, pattern).Cast<Match>();
var ascii_digit_cnt = ms.Select(x => x.Groups[1].Value).Where(n => !string.IsNullOrEmpty(n)).Count();
var ascii_letter_cnt = ms.Select(x => x.Groups[2].Value).Where(n => !string.IsNullOrEmpty(n)).Count();
var han_cnt = ms.Select(x => x.Groups[3].Value).Where(n => !string.IsNullOrEmpty(n)).Count();
Console.WriteLine($"{ascii_digit_cnt} : {ascii_letter_cnt} : {han_cnt}");
// => 6 : 39 : 4

See the C# demo

At first, you get all matches with Regex.Matches(s, pattern).Cast<Match>(). Then, you have ASCII digit matches in x.Groups[1], ASCII letter matches in x.Groups[2] and Han chars in x.Groups[3]. The .Where(n => !string.IsNullOrEmpty(n) removes all empty values (as those mean the group pattern did not match).

Upvotes: 1

Related Questions