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.
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'
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.
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