Extract and count particular word present between two constant word

Hi,
I have a data in which I have to count the number of particular words present in between two constant word .
How can we achive this using regex
The sample data is given below.

Then account is pending
Type of control control Deficiency
Major abcd xyz
Moderate Benefits etc.
Major account number
Minor Sample paper
Extract minor change then
Detailed report
Wording of stopping

In the above Type of control and Detailed report are constant.in between these 2 words there are the word “Major,Minor,Moderate.”
I need to take the count of the word Major,Moderate and Minor

Sample Output is given below
Major=2
Minor=2
Moderate=1

Hi @yashashwini2322

you can try using regex

System.Text.RegularExpressions.Regex.Matches(inputstr,“Major”).count
output =2

System.Text.RegularExpressions.Regex.Matches(inputstr,“Minor”).count
output =1
System.Text.RegularExpressions.Regex.Matches(inputstr,“Moderate”).count
output=1

@yashashwini2322

Matches Activity (Input: Your Sample Data, Pattern: (?<=Type of control)(.*?)(?=Detailed report))

For Each (TypeArgument: System.Text.RegularExpressions.Match)
Assign: majorCount = System.Text.RegularExpressions.Regex.Matches(item.ToString(), “Major”).Count
Assign: minorCount = System.Text.RegularExpressions.Regex.Matches(item.ToString(), “Minor”).Count
Assign: moderateCount = System.Text.RegularExpressions.Regex.Matches(item.ToString(), “Moderate”).Count

Write Line: “Major=” + majorCount.ToString
Write Line: “Minor=” + minorCount.ToString
Write Line: “Moderate=” + moderateCount.ToString

Hi @yashashwini2322 ,

Maybe we could also store it in Dictionary and access By Key the Count.

  1. Firstly, Capturing the required text from the Input Text :
MatchValue = Regex.Match(strVar,"(?<=Type of control)[\s\S]+(?=Detailed report)").Value

Here, MatchValue is a String type variable and strVar is the Input text.

  1. Next, Capturing the Count and Mapping it with the 3 Keys :
    We could use the below Regex Expression for capturing the matches :
\b(Major|Minor|Moderate)\b

Expression for capturing the Word Count and converting to a Dictionary :

WordCountDict = Regex.Matches(MatchValue,"\b(Major|Minor|Moderate)\b",RegexOptions.IgnoreCase).GroupBy(Function(x)System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x.Value)).ToDictionary(Function(x)x.Key,Function(y)y.Count)

Here, WordCountDict is a Dictionary variable of Type Dictionary(Of String,Integer)

Debug Visuals :

image

It’s throwing an error.

@yashashwini2322 ,

Could you let us know if you’re using Windows or Windows-Legacy Compatibility ?

Also, try with the below Modified Expression :

Regex.Matches(MatchValue,"\b(Major|Minor|Moderate)\b",RegexOptions.IgnoreCase).Cast(Of Match).GroupBy(Function(x)System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x.Value)).ToDictionary(Function(x)x.Key,Function(y)y.Count)

@yashashwini2322 ,

Maybe the Suggestion of Dictionary Storage provided was wrong for your case as we see you have created another Topic on the same :

If the requirement is to create Separate Variables, then we can check with the below Expressions :

Major = Regex.Matches(MatchValue,"\bMajor\b",RegexOptions.IgnoreCase).Count
Minor = Regex.Matches(MatchValue,"\bMinor\b",RegexOptions.IgnoreCase).Count
Moderate = Regex.Matches(MatchValue,"\bModerate\b",RegexOptions.IgnoreCase).Count

Here, Minor, Major and Moderate are Integer type variables.

Let us know if this does not work for you and maybe explain a bit more to understand the exact need.