Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to print a regex matched string?

Tags:

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?

like image 799
minerals Avatar asked Sep 12 '13 10:09

minerals


People also ask

How do I print a regular expression?

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.

How do you check if a string matches a pattern in Python?

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.

What is pattern match return Python?

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.


2 Answers

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.

like image 91
Chris Seymour Avatar answered Sep 17 '22 03:09

Chris Seymour


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
like image 45
poke Avatar answered Sep 18 '22 03:09

poke