简体   繁体   English

正则表达式从x删除到y

[英]Regex Remove From x To y

I'm new to regex, I know the basics but only the basics. 我是regex的新手,我了解基础知识,但仅基础知识。 I need to parse a string to remove all occurances of one string to another. 我需要解析一个字符串以将一个字符串的所有出现都删除到另一个字符串。 For example, 例如,

Here is some random text
This wants to stay
foo
This wants to be removed 
bar
And this wants to stay

So the desired output would be 所以所需的输出将是

Here is some random text
This wants to stay
And this wants to stay

And removed would be 并删除将是

foo
This wants to be removed
bar

It will always follow the pattern of match 'this string' to 'that string' and remove everything in between, including 'this string' and 'that string'. 它将始终遵循将“此字符串”匹配到“该字符串”的模式,并删除两者之间的所有内容,包括“此字符串”和“该字符串”。

The file is a text file, for the sake of this question, the pattern will always start with foo and end with bar, removing foo, bar and everything in between. 该文件是一个文本文件,出于这个问题的考虑,该模式将始终以foo开头,以bar结尾,并删除foo,bar及其之间的所有内容。

Foo and Bar ARE part of the file and need removing. Foo和Bar是文件的一部分,需要删除。

Regexes are probably the wrong tool here. 正则表达式可能是错误的工具。 I'd probably use string equality along with the flip-flop operator. 我可能会在触发器运算符中使用字符串相等性。

while (<$input_fh>) {
  print $output_fh unless ($_ eq "foo\n" .. $_ eq "bar\n");
}

You could do it with a regex and a match operator. 您可以使用正则表达式和匹配运算符来实现。

while (<$input_fh>) {
  print $output_fh unless /foo/ .. /bar/;
}

That looks neater, but the regexes will match if the strings appear anywhere on an input line. 看起来更整洁,但是如果字符串出现在输入行的任何位置,则正则表达式将匹配。

Update: Inverted the logic on the tests - so it's now correct. 更新:颠倒了测试的逻辑-因此现在是正确的。

That's not what RegEx is there for. 那不是RegEx的目的。 RegEx is there to detect pattern - if you want simple string slice, you should simply iterate over the big string with a simple comparison (or, with other languages which include string operations, indexOf("your string here"); etc. ) RegEx可以检测模式-如果您想要简单的字符串切片,则应使用简单的比较简单地遍历大字符串(或使用其他包括字符串操作的语言,在indexOf("your string here");等中进行迭代)

However, simple typing of the string would find you the matches: This wants to be removed will return all occurances of that specific string, and thus it is fit for you. 但是, This wants to be removed键入字符串即可找到匹配项: This wants to be removed该字符串,将返回该特定字符串的所有匹配项,因此很适合您。

Are you looking for something like this? 您是否正在寻找这样的东西?

#!/usr/bin/perl
$start = "foo";
$end = "bar";
while (<STDIN>) {
$str = $str . $_;
}
$str =~ s/(.*)$start\n.*$end\n(.*)/\1\2/s;
print $str;

The only part of real importance to you is the regex I suppose, but I declare the start and end, then read from standard input and tack each concurrent line onto $str. 对您来说真正重要的唯一部分是我想的正则表达式,但是我声明了开始和结束,然后从标准输入中读取并将每条并发的行添加到$ str上。 Then I take str and say "whatever is the first thing within perenthesis before foo put first, whatever is in the second after bar parenthesis put last" (with the backslash \\1 and \\2) 然后我接受str并说“在foo放在第一位之前,perenthesis中的第一件事是什么,在bar括号最后放在第二位之后是什么呢”(反斜杠\\ 1和\\ 2)

My output from a file containing your lines is: 我从包含您的行的文件中得到的输出是:

marshall@marshall-desktop:~$ cat blah | ./haha 
Here is some random text
This wants to stay
And this wants to stay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM