【问题标题】:python regex find all single qutoes inside single quotespython 正则表达式查找单引号内的所有单引号
【发布时间】:2016-03-01 09:38:35
【问题描述】:

你能帮我用正则表达式找到单引号内的所有单引号吗?

IE

'sinead o'connor','don't don't','whatever'

感谢您的建议。

【问题讨论】:

    标签: python regex single-quotes


    【解决方案1】:

    好像你的字符串是用逗号分隔的。

    re.sub(r"\b'\b", "''", s)
    

    (?<=[^,])'(?!,|$)
    

    DEMO

    例子:

    >>> import re
    >>> s = "'sinead o'connor','don't don't','whatever'"
    >>> re.sub(r"\b'\b", "''", s)
    "'sinead o''connor','don''t don''t','whatever'"
    >>> 
    

    【讨论】:

    • 您好 Avinash,感谢您的回复。你知道如何构建正则表达式来查找单引号之间的所有单引号吗?
    【解决方案2】:

    即使没有正则表达式,您也可以做到这一点:

    >>> string = "'sinead o'connor','don't don't','whatever'"
    >>> string = string.replace("'", "''")
    "''sinead o''connor'',''don''t don''t'',''whatever''"
    >>> string.strip("'")
    "sinead o''connor'',''don''t don''t'',''whatever"
    

    【讨论】:

      【解决方案3】:

      试试这个:

      '[\w|\s]*([']*)[\w|\s]*'
      

      regex101.com上查看这个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-07
        • 1970-01-01
        相关资源
        最近更新 更多