Regex.Matches count. Same word, only one have a space after

Hi,

How do I best count how many of a word in a string.

my_str = “11mr, 11mr ,11mr”
word_cnt = Regex.Matches(my_str,“11mr”).Count()

This will get me word_cnt = 3, hur do I make sure it only counts exactly “11mr” and not whit a space behind it.
So in this case I would want word_cnt to be 2.

//BR S

@Sven_son

Please try this

(?<! )11mr(?! )

Cheers

Hi @Sven_son - I’m trying to understand more better, you’re trying to get the 11mr count in your input string. Since there were 3 11mr, it is giving the count as 3. I guess it’s just not because of the whit space. Instead of that if you need the count as 2 then try this with Assign activity

word_cnt = word_cnt - 1 

The above one will give the value as 2

Hi,

I need to know how many "11mr " there is in the string to be able to know that it should be -1 in the next assing.

Next time Im running the script my_str could be "11mk , 11mk , 11mk, 11mk, 11mk, 11mk ".
So what I want to do is find the exakt count for “11mk” and dont count "11mk " in the counter.
So in the above i would like the result to be 3.

BR S

Hi, I havent used the regex activity yet, but ill try and see if I can get it to work.

Thanks in advance.

Update:
It worked, thanks a lot :slight_smile:
BR C

1 Like

[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.