Question on count the number of match string by regex

I want to count the number of a regex pattern exist in a string. The regex pattern is “#\d{8}”.

How can I do that?

@Emily_Yip
System. Text.RegularExpressions.Regex.Matches(text,"#\d{8}").Count

1 Like

@Emily_Yip try this use your regex,matches
then store the result in MatchCollection then you can use tat varible to get the count yourMatchCollection _Varible.count

@Emily_Yip Refer this link

enumerable.Count(matchesOutputVariable).ToString

@Emily_Yip

Thanks!

2 Likes

Thanks @kadiravan_kalidoss

And I have a string of #111111111#22222222
and I want to extract string of 8 digit after #.
Val = 11111111, val2 = 22222222 and store into list.

Do anyone has idea how to do that?

(?<=#)\d{8}

Hello @Emily_Yip

Yes you can do that by Casting the matches value you get…

Assign This code to a List<of String> datatype variable

System.Text.RegularExpressions.Regex.Matches(Strrr,"(?<=#)\d{8}").Cast(of match).Select(Function(r) (r).tostring ).tolist

This will give you all the values in a list of string

If you want it in an integer you just have to add Convert to method just like this

System.Text.RegularExpressions.Regex.Matches(Strrr,"(?<=#)\d{8}").Cast(of match).Select(Function(r) Convert.ToInt32(r) ).tolist

Check it out and let me know about it