开发者社区> 问答> 正文

正则表达式-后向搜索需要固定宽度的模式错误

码:

text = "('hel'lo') eq 'some 'variable he're'"
re.compile(r"(?<!\(|(?<=eq ))'(?!\)|\Z)").sub(string=text, repl="''")

获取错误:

re.error: look-behind requires fixed-width pattern

预期输出

('hel'lo')eq'一些'variable he're'

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 16:47:09 413 0
1 条回答
写回答
取消 提交回答
  • 如果要断言左边不是eq,则应该是负向后((?<!`)而不是正向后。

    您可以使用2个后向断言来编写模式。

    (?<!\()(?<!eq )'(?!\)|\Z)
    

    正则表达式演示| Python演示

    范例程式码

    import re
    text = "('hel'lo') eq 'some 'variable he're'"
    print(re.compile(r"(?<!\()(?<!eq )'(?!\)|\Z)").sub(string=text, repl="''"))
    

    输出量

    ('hel''lo') eq 'some ''variable he''re'
    

    回答来源:stackoverflow

    2020-03-23 16:47:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载