[英]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])|([\一-\龥])"
I want to get the three match group count:pattern1 [0-9]
the number is 6; The [\一-\龥]
count is 4,Is there possible to calculate that. 这是我使用的正则表达式模式
([0-9])|([a-zA-Z])|([\一-\龥])"
我想获得三个匹配组计数:pattern1 [0-9]
数字为6; [\一-\龥]
计数为4,是否有可能计算出来。
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? 但我只得到所有的比赛数是49.那么,是否有可能得到不同的部分数?
What I expected to get is that the match the [0-9]
count, match the [a-zA-Z] count 我期望获得的是匹配
[0-9]
计数,匹配[a-zA-Z]计数
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) 注意:要在c#online上测试正则表达式,我使用http://regexhero.net/tester/(Silverlight仅在IE ... O_o中)
You need to count all Group 1, 2 and 3 capture group values that are not empty: 您需要计算非空的所有第1组,第2组和第3组捕获组值:
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
At first, you get all matches with Regex.Matches(s, pattern).Cast<Match>()
. 首先,您将获得与
Regex.Matches(s, pattern).Cast<Match>()
所有匹配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]
. 然后,你必须在ASCII数字匹配
x.Groups[1]
,ASCII字母匹配x.Groups[2]
汉族字符x.Groups[3]
The .Where(n => !string.IsNullOrEmpty(n)
removes all empty values (as those mean the group pattern did not match). .Where(n => !string.IsNullOrEmpty(n)
删除所有空值(因为那些意味着组模式不匹配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.