RegEx Expression to count a WORD in LINES
-
If I have 1000 lines of text, and a word MILKis found 1100 times in total, but the word MILK is not in all lines, because it is found several times in other lines.
Is there a RegEx Expression to count the amount of lines in which the word MILK appears?
-
Sure, try:
(?-s)^.*?MILK.*?\RPress the Count button.
Make sure the last line has a line-ending on it if your word appears on that line and you want it counted. Sure would be nice to have a native option to ALWAYS put a line-ending on that last line!
-
Hi, @elija-5801 and All,
Very easy to achieve, indeed ! Let’s suppose we have the following text and you would like to search for the word
test:This is a test, a good test but a simple test test A simple line The test to test A test to do test Simple test which have not been tested, yet test test test To determine The number of lines test-
First, open a new tab (
Ctrl + N) ( Leave the last linetestwithout any line break ) -
Copy / paste, the text. Then,
-
Open the Find dialog (
Ctrl + F) -
Select the
Regular expressionsearch mode -
Type in the regex
(?-s).*\btest\b, in order to know how many lines contain the wordtest, in the current file
OR
-
Type in the regex
\btest\b, in order to know the number of occurrences of the wordtest, in the current file -
Click on the Count button
=> The result appears, at the bottom of the Find dialog
Et voilà !
guy038
P.S. :
- Note that if we have searched for the string
test, even glued in words, with the regexes :
SEARCH
(?-s).*testSEARCH
testWe would have obtained one more line and one more occurrence, because of the line “
been tested, yet” !- See also my reply to your recent post :
-
-
I’m so appreciate this forum, the pros have solved two RegEx problems for me this weekend.
Thanks to Scott and Guy038.
Interestingly both your methods works perfectly.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login