【问题标题】:Notepad++: Search and Replace with Regular ExpressionNotepad++:使用正则表达式进行搜索和替换
【发布时间】:2013-10-31 19:10:20
【问题描述】:

我在 Notepad++ 中有数千个条目(在其他文本中混入),我试图删除任意 2 个数字之间的破折号。

我有这样的数据:

text text this is text here
234-5678
this is more text here 
123-4585 this is more text here
or text is here too 583-5974

期望的结果:

text text this is text here
2345678
this is more text here 
1234585 this is more text here
or text is here too 5835974

我试过了:

Find: [0-9]-[0-9] (which works to find the (#)(dash)(#)
Replace: $0, $1, \1, ^$0, "$0", etc.

【问题讨论】:

    标签: regex notepad++


    【解决方案1】:

    试试这个正则表达式。

    (?<=\d)-(?=\d)
    

    【讨论】:

    • 天才!谢谢,完美运行。对于其他用户,这是您想要的 FIND 表达式,然后 REPLACE 文本将为空白(删除破折号)。谢谢edi_allen
    【解决方案2】:

    你的方法有什么问题:

    Find: [0-9]-[0-9] (which works to find the (#)(dash)(#)
    Replace: $0, $1, \1, ^$0, "$0", etc.
    

    $0$1 等是指捕获的组,而您的 find 正则表达式没有。如果你这样做了,它会起作用的:

    Find: ([0-9])-([0-9])
    Replace: $1$2
    

    $1 将包含第一个数字,$2 包含第二个数字。

    当然现在,使用像edi_allen's answer 这样的lookarounds((?&lt;= ... ) 是一个正向lookbehind,(?= ... ) 是一个正向lookahead)也可以工作并且避免使用反向引用的麻烦($1$2 )。

    $0 包含整个匹配项。 $1 包含第一个捕获的组,$2 包含第二个捕获的组。

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 2011-01-08
      • 2020-02-02
      • 2019-01-27
      • 2016-11-25
      相关资源
      最近更新 更多