简体   繁体   English

python 正则表达式匹配所有文字字符之间的字符串

[英]python regex to match string between all literal characters

i am new to python regex i am trying to write a regular expression to match a string between character which are literals我是 python 正则表达式的新手 我正在尝试编写一个正则表达式来匹配文字字符之间的字符串

my_string = "```,null,")]}'\n[[\"102fb5\",\"```"

What I want is 102fb5 .我想要的是102fb5

The regex I have written:我写的正则表达式:

"```n\[\[\\\"((?:.|\n)*?)\\"```"

First of all, I would recommend you use a Python raw string, it would make your RegEx more readable, in the sense that you won't need to escape every backslash:首先,我建议您使用 Python 原始字符串,这将使您的 RegEx 更具可读性,因为您不需要转义每个反斜杠:

QUERY = r'```n\[\[\"((?:.|\n)*?)\"```'

However, I'm not sure why you're bothering to match the entire list-like structure, when you could just search for the "literals" themselves by identifying their quotation marks:但是,我不确定您为什么要费心匹配整个类似列表的结构,而您只需通过识别引号来搜索“文字”本身:

# \"(.*?)\"
# Match escaped quotation mark: \"
# Match the literal text: .*?
# Match the other escaped quotation mark: \"

>>> re.findall(r'\"(.*?)\"', '''```,null,")]}'\n[[\"102fb5\",```"'\n[[\"102fb5\",\"```''')

['102fb5', '102fb5']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM