I want to match a part of the string (a particular word) and print it. Exactly what grep -o
does.
My word is "yellow dog" for example and it can be found in a string that spans over multiple lines.
[34343] | ****. "Example": <one>, yellow dog
tstring0 123
tstring1 456
tstring2 789
Let's try this regex mydog = re.compile(', .*\n')
and then
if mydog.search(string):
print only the matched words.
How do I get only "yellow dog" in the output?
How to print all the characters of a string using regular expression in Java? Compile the regular expression using the compile() method. Create a Matcher object using the matcher() method. Find the matches using the find() method and for every match print the matched contents (characters) using the group() method.
Method : Using join regex + loop + re.match() In this, we create a new regex string by joining all the regex list and then match the string against it to check for match using match() with any of the element of regex list.
The match method returns a corresponding match object instance if zero or more characters at the beginning of the string match the regular expression pattern. In simple words, the re. match returns a match object only if the pattern is located at the beginning of the string; otherwise, it will return None.
Using a capture group and findall:
>>> import re
>>> s = """[34343] | ****. "Example": <one>, yellow dog
... tstring0 123
... tstring1 456
... tstring2 789"""
>>> mydog = re.compile(', (.*)\n')
>>> mydog.findall(s)
['yellow dog']
If you only want the first match then:
>>> mydog.findall(s)[0]
'yellow dog'
Note: you'd want to handle the IndexError
for when s
doesn't contain a match.
If you don’t specify a capture group, the text that is matched by the whole expression will be contained withing matchResult.group(0)
. In your case, this would be ', yellow dog\n'
. If you just want the yellow dow
, you should add a capture group to the expression: , (.*?)\n
. Note that I also changed the .*
into a .*?
so that it will be non-greedy and stop when it finds the first line break.
>>> s = '''[34343] | ****. "Example": <one>, yellow dog
tstring0 123
tstring1 456
tstring2 789'''
>>> mydog = re.compile(', (.*?)\n')
>>> matchResult = mydog.search(s)
>>> if matchResult:
print(matchResult.group(1))
yellow dog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With