【问题标题】:Regex for dot, space and newline点、空格和换行符的正则表达式
【发布时间】:2014-05-23 09:56:58
【问题描述】:

我正在尝试为 python 编写一个正则表达式,用一个逗号替换点、空格或换行符(以及这些的任何组合)。我不明白为什么我的正则表达式不起作用。

    newline = line.replace("[\. \\n]+",",")

【问题讨论】:

    标签: python regex


    【解决方案1】:

    您需要使用sub 才能在搜索替换中使用正则表达式。

    # your code goes here
    import re
    line = "something with space . dot";
    line = re.sub(r'[. \n]+', ",", line);
    print line;
    

    Demo

    【讨论】:

      【解决方案2】:

      方括号(“字符类”)之间的字符是文字​​,所以不需要转义。请改用[. \n]+

      编辑

      根据这个答案Python string.replace regular expressionreplace不识别正则表达式,你需要使用sub

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 2020-09-12
      • 2019-06-21
      相关资源
      最近更新 更多