Just a revision for me, sharing with you all!
so i have problem to find multiple keywords in a text. i tried using re.search. but that wouldnt work.
as re.search only applicable if keyword found first in a group match.
so, lets go ahead!
i ran using python2.7
Example code for find keywords appearance using python re.finditer (regex)
Now we join the keywords with OR operator | , it will looks something like this
smart|high|confident|mature
Sample input text
Now we update the pattern again with ( ) - > parenthesis
The object is to capture the keyword as a group not individual character. it will looks like
(smart|high|confident|mature)
To find all pattern matches we need to use finditer function. because findall/search will
only return the first match of the keyword in (smart|high|confident)
If we use online regex parser,
we can see that to achieve the same thing we need to apply "Global" flag. Unfortunately python
doesnt support that.
To check individual match result, iterate and print m.group()
When you ran, you will get this result
Try out too!, i did post the note in git. you may refer there
so i have problem to find multiple keywords in a text. i tried using re.search. but that wouldnt work.
as re.search only applicable if keyword found first in a group match.
so, lets go ahead!
i ran using python2.7
Example code for find keywords appearance using python re.finditer (regex)
search_keywords = ["smart", "high", "confident", "mature"]
Now we join the keywords with OR operator | , it will looks something like this
smart|high|confident|mature
search_patterns = "|".join(search_keywords)
Sample input text
text = "This is a story about a girl. Living in a secluded area in Wonderland.\n" \
"She is small but very smart and smart, how ever she has verly low confident. Dont judge a book" \
"by its cover.\n The way she thinks shown how mature she is."
Now we update the pattern again with ( ) - > parenthesis
The object is to capture the keyword as a group not individual character. it will looks like
(smart|high|confident|mature)
search_patterns = "(%s)" % search_patterns
regex = re.compile(search_patterns)
To find all pattern matches we need to use finditer function. because findall/search will
only return the first match of the keyword in (smart|high|confident)
If we use online regex parser,
we can see that to achieve the same thing we need to apply "Global" flag. Unfortunately python
doesnt support that.
it = regex.finditer(text, re.MULTILINE)
To check individual match result, iterate and print m.group()
for match in it:
print match.group()
When you ran, you will get this result
smart
smart
confident
mature
Try out too!, i did post the note in git. you may refer there
Comments
Post a Comment