user2905592
user2905592

Reputation: 65

Extract text from Regular Expression?

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.

Upvotes: 4

Views: 130

Answers (2)

Simon
Simon

Reputation: 10841

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.

Upvotes: 2

Tom Jacques
Tom Jacques

Reputation: 705

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'

Upvotes: 6

Related Questions