【问题标题】:regular expression for left bracket three digits right bracket i.e. [123] or [368] and replace with ''左括号三位数右括号的正则表达式,即[123]或[368]并替换为''
【发布时间】:2017-03-12 13:51:57
【问题描述】:

我正在寻找一个正则表达式,它将匹配 5 个字符串,如标题中显示的两个字符串。这是一个示例输入字符串。

This is a sentence that doesn't contain any matches of the regex.  
This is a sentence that has two matches of the string at the end of the sentence [411] [101].  
This is a sentence that has three matches [876] [232] [323].

我希望在 perl 或 sed 中看到从文本文件中删除这些字符串的解决方案,以及从短字符串中简单地删除此字符串的解决方案。我是正则表达式、perl 和 sed 的新手。我尝试使用反向正则表达式工具,它似乎给了我这个正则表达式,但我找不到将它与 perl 或 sed 一起使用的方法。

\\[\\d\\d\\d\\]

然后我用 perl 尝试了类似的东西,但没有更进一步。

perl -p -i -e 's/\\[\\d\\d\\d\\]/""/g' textFileToRemoveRegexMatches.txt

【问题讨论】:

    标签: regex perl replace sed


    【解决方案1】:

    尝试下一个:

    my $str = 'word [123] word [456]';
    my $regex = qr/\[\d{3}\]/p;
    my $subst = '';
    
    my $result = $str =~ s/$regex/$subst/rg;
    

    但也许您想使用命令sed。例如

    sed 's/\[\d{3}\]//g' filename.txt
    

    【讨论】:

      【解决方案2】:

      Perl 中的解决方案:

      $ echo 'one[876] two[232] three[323]' | perl -pe 's/\[\d{3}\]//g;'
      

      打印:

      one two three
      

      Sed 中的解决方案:

      $ echo 'one[876] two[232] three[323]' | sed 's/\[[[:digit:]]\{3\}\]//g;'
      

      打印:

      one two three
      

      这些示例使用了实时命令行界面,但您也可以将代码放入脚本文件以供重复使用,如下所示:

      Perl 脚本:

      #! /usr/bin/perl -p
      # purge-bracket-numbers.perl
      s/\[\d{3}\]//g
      

      Sed 脚本:

      #! /usr/bin/sed -f
      # purge-bracket-numbers.sed
      s/\[[[:digit:]]\{3\}\]//g
      

      【讨论】:

        【解决方案3】:

        这个怎么样:

        >>> s = "Hello world [123] this is some text"
        >>> e = r'\[\d{3}\]'
        >>> import re
        >>> re.sub(e, '', s)
        'Hello world  this is some text'
        

        如果您想大规模执行此操作,请考虑使用 sed,它是 stream editor。除了作为 macOS 上的核心实用程序之外,它还适用于所有 Linux 风格。

        我用这两行创建了一个示例文件:

        This is line one with [123] and needs to be substituted.
        This is a longer line, lets call it line 2 that has [this thing] that should not be replaced, but [345] that should.
        

        您使用 sed 的方式是传递一个替换表达式。命令s 表示替换,g 表示替换所有匹配项,而不仅仅是第一个匹配项。

        接下来,您将要搜索的表达式和中间替换为字符。常见的规范是使用/,但您可以使用任意两个在您的shell 中没有特殊含义的相似字符。

        所以,sed 命令是:

        sed s/search-for-this/replace-with-this/g the-name-of-the-file.txt
        

        如果您键入以上内容,sed 将简单地返回它所替代的内容。这是我们的正则表达式的示例:

        $ sed 's/\[[0-9]\{3\}\]//g' test.txt
        This is line one with  and needs to be substituted.
        This is a longer line, lets call it line 2 that has [this thing] that should not be replaced, but  that should.
        

        sed 的默认行为是返回结果;并且它不会修改原始文件(因为它旨在处理流)。

        要让 sed 更改原始文件,请传递 -i 参数,这意味着 就地 - 也就是说,在文件本身中进行替换,如下所示:

        $ sed -i 's/\[[0-9]\{3\}\]//g' test.txt
        

        请注意,这一次,它没有返回任何内容,但是,如果我们检查它已被修改的文件:

        $ cat test.txt
        This is line one with  and needs to be substituted.
        This is a longer line, lets call it line 2 that has [this thing] that should not be replaced, but  that should.
        

        注意:如果你使用的是 mac,你可能需要使用sed -i '.bak'

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-03
          • 1970-01-01
          • 2020-01-07
          • 2016-11-30
          相关资源
          最近更新 更多