技巧

Python 3.15 将软弃用 re.match() 函数

Soft-deprecating re.match()

精选理由

Python 新版要弃用一个老函数,推荐你看看,它用 re.prefixmatch() 替代了 re.match(),这样写代码更清晰。

Python 3.15 版本将软弃用 re.match() 函数,这是 Python 中一个用于从字符串开头匹配正则表达式的函数。该函数将被替换为更清晰的 re.prefixmatch(),后者同样从字符串开头匹配但不会匹配到结尾。通常情况下,应该使用 re.search()(匹配任意位置)或 re.fullmatch()(匹配整个字符串)。

原文 · Simon Willison’s Weblog

Soft-deprecating re.match()

Soft-deprecating re.match() Python has a concept of soft deprecation , where APIs are marked as "should no longer be used to write new code" without any promise/threat to remove them in the future. Python 3.15 release manager Hugo van Kemenade describes how in the upcoming 3.15 release soft deprecation has come for the venerable but deeply confusing re.match() function. It's now available with the much clearer alternative re.prefixmatch() name - reflecting how it anchors at the beginning of the string but not the end. Most of the time you probably want re.search() (match this pattern anywhere in the string) or re.fullmatch() (match the entire string) instead. Tags: python , regular-expressions