使用正则表达式解析python字符串

Bin*_*hen 1 python regex

给定一个字符串#abcde#jfdkjfd,我如何获得两个字符串之间的字符串#?我也希望如果没有#对(意味着没有#或只有一对#),函数将返回None.

Tim*_*ker 7

>>> import re
>>> s = "abc#def#ghi#jkl"
>>> re.findall(r"(?<=#)[^#]+(?=#)", s)
['def', 'ghi']
Run Code Online (Sandbox Code Playgroud)

说明:

(?<=#)  # Assert that the previous character is a #
[^#]+   # Match 1 or more non-# characters
(?=#)   # Assert that the next character is a #
Run Code Online (Sandbox Code Playgroud)