Python Tip #95 (of 365):

Don't use re.match(): it's confusing.

I'm not sure I've ever seen re.match() used when it wasn't being used by mistake.

If you think you want re.match(), you probably want either re.ā€search() or re.fullmatch() instead.

When matching a regex against a string, we're usually either trying to:

1. Find a regular expression within a string
2. Match a regular expression against a whole string

re.match() does neither of those!

🧵 (1/3)

#Python #DailyPythonTip

To find a match WITHIN a string, you can use re.ā€search().

To match an entire string against a regex, you can use re.fullmatch().

Using re.fullmatch() is the same as using re.ā€search() with ^ and $ used to anchor to the beginning and end of the string.

In other words, these are equivalent:

match = re.ā€search(r"^\w+$", string)
match = re.fullmatch(r"\w+", string)

re.match() is a weird middle ground between re.ā€search() and re.fullmatch().

🧵 (2/3)

But why are they named this way?

Wouldn't it be simpler if re.fullmatch() was called re.match() or re.ā€search() was called re.match() and the current behavior of re.match() didn't exist at all?

I think so. But it's too late for that.

This is all confusing due an accident of history.

re.fullmatch() didn't used to exist but re.match() did. By the time they invented re.fullmatch(), the name "match" was already taken.

Personally, I usually just stick with re.ā€search().

🧵 (3/3)

@treyhunner In 3.15 we've added an re.prefixmatch() as an explicit alias to the confusing re.match().

https://docs.python.org/3.15/library/re.html#prefixmatch-vs-match
#Python #python315 #regex

re — Regular expression operations

Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...

Python documentation
@hugovk @treyhunner Woah, I love this!! The behavior of match() versus search() has me never using match() ever.
@sethmlarson @hugovk @treyhunner always start your python regexes with rā€š.*ā€˜. šŸ˜