Click here to Skip to main content
14,880,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
MatchCollection collection2 = Regex.Matches(range.Text, @"(\W|^)u/s.(\W|$)");
               count = collection2.Count;


I need to find all the "u/s." in my file, i'm using this code...
in actual there are 41 occurrence but this code is counting only 15...
what to do, plz help
Posted
Updated 26-Jun-14 1:10am
v2

try with
C#
 string pattern = Regex.Escape("u/s.");
MatchCollection collection2  = Regex.Matches(range.Text, @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + pattern + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", RegexOptions.IgnoreCase);
   
v2
Comments
ShobuGpta 26-Jun-14 5:38am
   
nope, still same ans..
DamithSL 26-Jun-14 5:51am
   
check my updated answer
ShobuGpta 26-Jun-14 6:17am
   
i used the ans below given below, it's working properly, thans btw..
DamithSL 26-Jun-14 6:28am
   
you asking something else in your question. in future if someone else tried accepted answer to search specific word it will fail.
Note that you actually need to count occurrences of given string. that is not the same as counting words with same text. Change the Question for your requirement.
ShobuGpta 26-Jun-14 7:10am
   
got it.
Sergey Alexandrovich Kryukov 26-Jun-14 15:11pm
   
5ed.
—SA
Try this regex pattern:
@"u/s\."
   
v2
Comments
ShobuGpta 26-Jun-14 5:39am
   
thanks it worked..
DamithSL 26-Jun-14 5:53am
   
if you have u/dfgdfgdgfs.u/s.u/s.dfgdfg this will count as 2 even though there is no such WORD
CHill60 26-Jun-14 8:00am
   
True ... but "u/s." isn't a "word" either ... nor did the OP say it was

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900