I need the regex for removing easycode from Perl code.
I am using this regex but this is not correct
$message =~ s/<.+?>|&[a-z]+;|\[.+?\]/ /g;
This regex is removing all code in between [.+?\]
.
Please let me know if anyone can help
#!/usr/bin/perl
use strict;
use warnings;
my @test = ("[ nikon 400]", "[ nikon \$400 ]", "[quick_sort]", "[CODE]", "[QUOTE]", "[OTHER STUFF]", "[ test123!%&/(){} ]");
foreach my $line (@test){
$line =~ s/<.+?>|&[a-z]+;|\[[A-Z]+\]/ /g;
print "$line\n"
};
输出:
[ nikon 400]
[ nikon $400 ]
[quick_sort]
[OTHER STUFF]
[ test123!%&/(){} ]
我可以假设这确实是您正在寻找的。是否要合并多个条目之间的[]
like[quick sort]
将更改为[quick_sort]
?这不会是一个大问题。但是您必须向我们提供有关文本的其他信息(在括号之间),以便我们可以构建一个合适的正则表达式,而不仅仅是一个贪婪的野兽。
嗯,是的,这就是该\[.+?\]
部分匹配的内容。只需删除该部分(以及前面的交替运算符):
$message =~ s/<.+?>|&[a-z]+;/ /gi;
(我也认为你的正则表达式应该不区分大小写)