简体   繁体   English

带有转义单引号的单引号字符串上的Python正则表达式

[英]Python regular expression on single quoted string with escaped single quotes

Suppose that we have some input like this (it's an example, no matter if it makes sense or not): 假设我们有这样的输入(这是一个示例,无论是否有意义):

data = "(((column_1 + 7.45) * 3) <>    column_2 - ('string\'1' / 2))"

Well, I need to match a string, that starts and ends with ' and may contain escaped single quotes as example above, using Python re module. 好吧,我需要使用Python re模块匹配一个字符串,该字符串以'开头和结尾,并且可能包含上述转义的单引号。 So the result should be string\\'1 . 因此结果应为string\\'1 How can we achieve it? 我们如何实现呢?

EDIT: I am using the PLY library and the usage should be 编辑:我正在使用PLY库,用法应该是

def t_leftOperand_arithmetic_rightOperand_STRING(self, t):
    r'<regex>'
    t.lexer.pop_state()
    return t

I believe you have to account for the escape being escaped as well. 我相信您也必须考虑逃脱的逃生。

For that, you'd need '[^'\\\\]*(?:\\\\[\\S\\s][^'\\\\]*)*' 为此,您需要'[^'\\\\]*(?:\\\\[\\S\\s][^'\\\\]*)*'


Input 输入

'''Set 1 - this
is another
mul\'tiline
string'''
'''Set 2 - this
is' a\\nother
mul\'''tiline
st''ring'''

Benchmark: 基准测试:

Regex1:   '[^'\\]*(?:\\[\S\s][^'\\]*)*'
Options:  < none >
Completed iterations:   400  /  400     ( x 1000 )
Matches found per iteration:   9
Elapsed Time:    5.00 s,   4995.27 ms,   4995267 µs


Regex2:   '(?:[^'\\]|\\.)*'
Options:  < s >
Completed iterations:   400  /  400     ( x 1000 )
Matches found per iteration:   9
Elapsed Time:    7.00 s,   7000.68 ms,   7000680 µs

Additional regex (For a test only. As @ridgerunner says this could cause a backtracking problem) 其他正则表达式(仅用于测试。正如@ridgerunner所说,这可能会导致回溯问题)

Regex2:   '(?:[^'\\]+|\\.)*'
Options:  < s >
Completed iterations:   400  /  400     ( x 1000 )
Matches found per iteration:   9
Elapsed Time:    5.45 s,   5449.72 ms,   5449716 µs

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

相关问题 Python正则表达式将引用的字符串与转义的单引号相匹配 - Python regex to match quoted string with escaped single quotes 替换字符串中的单引号但不转义单引号 - Replace single quotes in a string but not escaped single quotes JSON 转储由单引号包围并包含转义单引号的字符串 - JSON Dump a string surrounded by single quotes and containing escaped single quotes Python正则表达式r前缀后跟三个单引号(或双引号) - Python regular expression r prefix followed by three single (or double) quotes 解析单引号或双引号并允许使用正则表达式转义字符(在Python中) - Parsing single or double quotes and allow for escaped characters using regular expressions (in Python) 混淆转义单引号原始字符串文字中的单引号 - Confusion escaping single quotes in a single-quoted raw string literal 如何用带引号的双引号替换字符串中的单引号 - How to replace single quotes within a string with quoted double quotes 使用正则表达式将 Python 中的转义双引号替换为单引号 - Replace escaped double quotes to single quotes in Python using regex 正则表达式匹配所有封闭&#39;&#39;(2单引号) - Regular Expression to Match All Enclosed '' (2 Single Quotes) 如何将双引号字符串转换为 python 中每个单独值的单引号? - How to convert a doube quoted string to single quotes for each indivdial value in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM