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

标签 python regex

给定一个字符串 #abcde#jfdkjfd,我怎样才能得到两个 # 之间的字符串?我还希望如果没有 # 对(意味着没有 # 或只有一个 #),该函数将返回 None.

最佳答案

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

解释:

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

关于使用正则表达式解析python字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559398/

相关文章:

javascript - 与 RTL 语言一起使用时字符串替换函数调用的顺序

python - 在 Python 中对列表进行切片

python - 建议使用 JupyterLab 构建并成功安装,但无法工作。为什么?

javascript - 正则表达式 javascript 帮助

regex - 使用脚本提取 Ubuntu 传感器命令

javascript - JS : matching entries with capture groups, 占新行

javascript - chrome.declarativeContent.PageStateMatcher 中的正则表达式

Python - 获取调用函数的完整文件路径?

python - Python 中的 String 是不可变的吗?

python - 做一些 python 分析后,Django(?)对大型数据集真的很慢