Hi there!
I want to count the number of times a specific word appears in an Excel.
I know there is this post Count Occurrence of word in OCR string but I don’t know where to declare it and it all seems very confusing.
I also have 2 screenshots of my work.
Could you please help me?
Thanks a lot.
Kindly follow the below steps that could resolve your issues
To check with number of words in a excel column, first use excel open application scope to open the excel
use Read range activity to read the sheet you want to and get the output with variable of type datatable named out_dt
Still being inside the scope, use a for each row loop with input of out_dt
now inside the for each row loop use a if condition with condition like
row(“ColumnName”).Contains(“yourword”)
if the condition passes, in then part of if condition use a counter of default value 0 initialized already like
in assign activity
counter = counter + 1
if not in else part leave as it is
This will give the count of the word in the particular column of a excel
use the following aString.Count(function(x) x = “theWord”). This returns an integer. You just have to replace ‘aString’ with the name of your variable and ‘theWord’ with the word you’re looking for.
There was a small confusion out there…my bad. I was trying to count the number of times a string appears in a text and write the result(the number of occurences) in the Excel cell.
I believe the regex will work great if I understand your problem. I’ve written the assignments below for the screenshot attached. The pattern will math only word (for example, if you set wordVariable to “word” it won’t match with “words”) and ignore the case.
pattern:
String.Format(“\b{0}\b”, wordVariable)
wordCount:
Regex.Matches(textToBeWritten, pattern, RegexOptions.Multiline Or RegexOptions.IgnoreCase).Count
You can directly put the followin as condition in you “If” activity:
Regex.Matches(textToBeWritten, String.Format(“\b{0}\b”, wordVariable), RegexOptions.Multiline Or RegexOptions.IgnoreCase).Count = 0
thx u both Tiberiu and msan. with your help guys I solved the problem. i still didn’t understand the whole thing with regex and how it works…do you have any recommendations for me to read more about it ? thx