I am trying to get the results of some consistent text in a regular expression, but it does not seem to work. Does anyone know what could be 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 does not print anything, i.e. no match found.
This is indeed a very common mistake - it seems you use re.matchit when you want to use it re.search. re.matchmatches only the beginning of the given text, while it re.searchchecks the whole thing.
re.match
re.search
Output:
'angle brackets'
@Tom Jacques , , , , , . :
import re text = "I want to match anything in <angle brackets>" match = re.search("\<(?P<brackets>.*)\>",text) if match: print (match.group('brackets'))
[^ .*) text re.search().
[^
.*)
text
re.search()
()
, , . > , >.
>