Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text from Regular Expression?

Tags:

python

regex

I'm trying to get the results of some matched text in a regular expression, but it doesn't seem to work. Anyone know what might be going wrong?

import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>[^\>]+)>", text)
if match:
    print (match.group('brackets'))

This prints nothing, i.e. no match found.

like image 742
user2905592 Avatar asked Feb 15 '23 22:02

user2905592


2 Answers

This is actually a really common error -- it looks like you're using re.match, when you wanted to use re.search. re.match only matches from the beginning of the given text, whereas re.search checks the entire thing.

import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>[^\>]+)>", text)
if match:
    print (match.group('brackets'))

Output:

'angle brackets'
like image 178
Tom Jacques Avatar answered Feb 27 '23 05:02

Tom Jacques


While @Tom Jacques has answered the question very nicely, the code shown in both the question and answer didn't work for me when I tried it. The following code worked:

import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>.*)\>",text)
if match:
    print (match.group('brackets'))

Note the replacement of the text [^ with .*) in the regular expression and the inclusion of the text parameter in the call to re.search().

(EDIT)

This answer addresses an issue that has since been corrected in both the question and the other answer. The change to the regular expression proposed here would capture all text up to the last > on the line, whereas the changed regular expression in the question and the other answer would capture text only up to the first > that it finds.

like image 36
Simon Avatar answered Feb 27 '23 03:02

Simon