We need to match either a number or a dash. Another approach would be to use a '|' and explicitly match either
a \d
or a literal dash.Consider the (
?(name-or-id)yes|no
) approach using a group number versus a group name. This lets us group for precedence without assigning to a numeric group.Frequently used regular expressions will perform much better if pre-compiled. This way, they do not need to go through the process each time.
The
match
method only matches at the beginning of a string, whereas search will scan a string for a match. The match method would make more sense when testing a small, discrete value such as an HTML form submission whereas search might be preferred when looking for a "needle in a haystack".The
finditer
method returns an interator whereasfindall
is going to return a list. The former uses less memory than the list construction.It's a non-standard Python extension. If you wish to use the expression elsewhere without modification, chances are you'll have to update it.