Python is very unintuitive compared to other languages for regular expression and string matching. . You have to import a separate library to get access to the functionality:
import re
Then the python man page is really confusing. It starts talking about compiling regular expressions. That may be something that you want to do when you’re doing a thousand searches, but mostly you just want to see if a string matches another string once. The JavaScript way of doing this is just STRING.match(STRING), but the python version is re.match(REGEX, STRING). Not only that, but there’s a HIDDEN CAVEAT in the python version – re.match only matches from the START of the string. I don’t know why this is the case, but the method you want to use is re.search(REGEX, STRING). This returns a SRE_Match object… ?? Apparently you use .group() on that object to get the actual match back… but even if you use a REGEX that should match multiple things in the string, it’ll only give you the first one… Why is this so difficult again??
If you want to avoid the re package AND you have a string that you want to find a match only at the BEGINNING or only at the END, then you can use STRING.startswith(STRING) or STRING.endswith(STRING) to return a True or False Boolean. Note that this doesn’t actually give you the match… it just tells you that it exists or not…
If you need to find multiple matches and you’re already importing re then you can use the findall method – re.findall(REGEX, STRING) , which strangely returns a list (of course)… though that’s better than returning an SRE_Match object I guess.