Python正则表达式:如何指定可选匹配(对于可能为空的子表达式)?

Has*_*ken 1 python regex string

我需要匹配以下输入集:

foo_abc_bar  
foo_bar
Run Code Online (Sandbox Code Playgroud)

并得到"abc"或空字符串作为结果.

所以这是我写的正则表达式:

r'foo_(abc|)[_|]bar'

但由于某种原因,这与我给出的第二个字符串不匹配.

在进一步检查时,我发现它[_|]与空字符串不匹配.

那么,我该如何解决这个问题呢?

NPE*_*NPE 5

要进行abc_选择,您可以使用问号运算符:

(abc_)?
Run Code Online (Sandbox Code Playgroud)

因此,整个正则表达式成为:

r'foo_(abc_)?bar'
Run Code Online (Sandbox Code Playgroud)

With this regex, the second underscore (if present) will become part of the capture group. If you don't want that, you could either remove it post-match with .rstrip('_') or use a slightly more complex regex:

r'foo_(?:(abc)_)?bar'
Run Code Online (Sandbox Code Playgroud)

I found that [_|] does not match an empty string.

That's right. Square brackets denote a character group. The [_|] would match exactly one underscore or exactly one vertical bar, and nothing else. In other words, the vertical bar loses its special meaning when it appears inside a character group.