我正在寻找一个正则表达式,它将匹配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发布于 2019-04-12 09:02:54
Perl中的解决方案:
$ echo 'one[876] two[232] three[323]' | perl -pe 's/\[\d{3}\]//g;'打印:
one two threeSed中的解决方案:
$ 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}\]//gSed脚本:
#! /usr/bin/sed -f
# purge-bracket-numbers.sed
s/\[[[:digit:]]\{3\}\]//ghttps://stackoverflow.com/questions/40325943
复制相似问题