我是正则表达式的新手,我知道基础但只有基础知识。我需要解析一个字符串以删除一个字符串到另一个字符串的所有出现。例如,
Here is some random text
This wants to stay
foo
This wants to be removed
bar
And this wants to stay
所以期望的输出是
Here is some random text
This wants to stay
And this wants to stay
删除将是
foo
This wants to be removed
bar
它将始终遵循匹配'this string'的模式为'that string'并删除其间的所有内容,包括'this string'和'that string'。
该文件是一个文本文件,为了这个问题,模式将始终以foo开头并以bar结束,删除foo,bar以及介于两者之间的所有内容。
Foo和Bar是文件的一部分,需要删除。
答案 0 :(得分:3)
正则表达式可能是错误的工具。我可能会使用字符串相等性和触发器操作符。
while (<$input_fh>) {
print $output_fh unless ($_ eq "foo\n" .. $_ eq "bar\n");
}
您可以使用正则表达式和匹配运算符来执行此操作。
while (<$input_fh>) {
print $output_fh unless /foo/ .. /bar/;
}
看起来比较整洁,但如果字符串出现在输入行的任何位置,则正则表达式将匹配。
更新:颠倒测试的逻辑 - 所以它现在正确。
答案 1 :(得分:0)
这不是RegEx的用途。 RegEx可以检测模式 - 如果你想要简单的字符串切片,你应该通过简单的比较(或者使用包含字符串操作的其他语言indexOf("your string here");
等)迭代大字符串。
然而,简单输入字符串会找到匹配项:
This wants to be removed
将返回该特定字符串的所有出现,因此它适合您。
答案 2 :(得分:0)
你在找这样的东西吗?
#!/usr/bin/perl
$start = "foo";
$end = "bar";
while (<STDIN>) {
$str = $str . $_;
}
$str =~ s/(.*)$start\n.*$end\n(.*)/\1\2/s;
print $str;
对你来说真正重要的唯一部分是我想的正则表达式,但是我声明了开始和结束,然后从标准输入读取并将每个并发行添加到$ str。然后我接受str并说“在foo放到第一个之前的任何东西中的第一个东西,无论是在第二个后面的条形括号放在最后”(使用反斜杠\ 1和\ 2)
包含您的行的文件的输出是:
marshall@marshall-desktop:~$ cat blah | ./haha
Here is some random text
This wants to stay
And this wants to stay