简体   繁体   English

python结合2个正则表达式,用于搜索单引号和双引号内的字符串

[英]python combining 2 regexes that search strings within single and double quotes

I have a regex that extracts everything between 2 double quotes and another regex that does the same for 2 single quotes. 我有一个正则表达式提取2个双引号之间的所有内容和另一个正则表达式对2个单引号进行相同的提取。 The strings within the quotes can include escaped quotes. 引号中的字符串可以包含转义的引号。 I'd like to make these 2 expressions into a single one: 我想将这两个表达式合并为一个:

1) re.findall(r'"(.*?)(? < !\\)"', string) 1)re.findall(r'“(。*?)(? < !\\)”',字符串)

2) re.findall(r"'(.*?)(? < !\\)'", string) 2)re.findall(r“'(。*?)(? < !\\)'”,字符串)

So something like: 所以像这样:

1+2) re.findall(r"'|\\"(? < !\\)['|\\"]", string) 1 + 2)re.findall(r“'| \\”(? < !\\)['| \\“]”,字符串)

but this isn't working. 但这不起作用。

I'd like to have 'abc\\"\\"' "abc\\'\\'" be evaluated using the same regex. 我想使用相同的正则表达式对'abc \\“ \\”'“ abc \\'\\'”“进行评估。 'abc\\"\\"" isn't expected to work. If the quotes were exchanged, allow the same regex to work on it also. Is it possible? 'abc \\“ \\”“不能正常工作。如果报价被交换,则允许同一个正则表达式也可以工作。可以吗?

not sure i understood exactly what you wanted but it is possible to reuse the value of captured group in a regex. 不确定我确切地了解您想要什么,但是可以在正则表达式中重用捕获的组的值。
may the following pattern do the job: 以下模式可以完成这项工作:
(['"])(.*)\\1 (['“])(。*)\\ 1

explanation: 说明:
(['"]) : a quote or double-quote is captured as first group (['“]) :引号或双引号被捕获为第一组
(.*) : the second group captures everything... (。*) :第二组捕获所有内容...
\\1 : ...until the first group value is met again \\ 1 :...直到再次满足第一个组值
the result is available in the second group 结果在第二组中可用

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

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