I'm trying to remove a specific character from a string in Perl:
我试图从Perl中的字符串中删除一个特定的字符:
my $string="MATTHATBAT";
substr($string, 2, 1, '');
EDIT: This does work, sorry. Leaving this here in case someone needs to know how to do this.
编辑:对不起,这是可行的。把这个放在这里以防有人需要知道怎么做。
Also, is there a more efficient way of doing this?
还有更有效的方法吗?
The string should now be MATHATBAT.
绳子现在应该是MATHATBAT。
Am I missing something? I know that I can use regex s///, but I am iterating through the string, looking for a certain character (this char changes), then removing the character (but only that character at that offset). So eventually, I will be removing the second or third occurence of the character (i.e. MATTHABAT, MATTHATBA and even MATHABAT etc)
我遗漏了什么东西?我知道我可以使用regex s///,但是我正在遍历字符串,寻找一个特定的字符(这个字符改变了),然后删除字符(但只删除那个偏移量中的字符)。所以最终,我将会删除角色的第二或第三个角色(即MATTHABAT, MATTHATBA,甚至MATHABAT等)
Can I maybe do this using search and replace? I am using a for loop to iterate through offsets.
我可以用搜索和替换吗?我使用for循环来迭代偏移量。
3 个解决方案
#1
2
Here is a benchmark comparing regexp vs substr:
这里有一个对比regexp和substr的基准:
#!/usr/bin/perl
use 5.10.1;
use warnings;
use strict;
use Benchmark qw(:all);
my $count = -3;
my $r = cmpthese($count,
{
'substring' => sub {
my $string = "MATTHATBAT";
substr($string, 2, 1, '');
},
'regexp' => sub {
my $string = "MATTHATBAT";
$string =~ s/(.{2})./$1/;
},
}
);
Result:
结果:
Rate regexp substring
regexp 162340/s -- -93%
substring 2206122/s 1259% --
As you can see, substr is about 13.5 times as fast as regex.
可以看到,substr的速度是regex的13.5倍。
@Sinan Ünür 1259% is 13.5 times and not 12.5 times.
@Sinan Unur 1259%是13.5倍而不是12.5倍。
#2
1
Your example does work. The $string
will contain MATHATBAT
as you wanted to have, the problem is somewhere else, not in this part.
你的例子。$string将包含您想要的MATHATBAT,问题在其他地方,而不是这一部分。
#3
0
You can loop the reg.exp matches with //g
你可以循环这个reg。exp匹配/ / g
from perlrequick :
从perlrequick:
$x = "cat dog house"; # 3 words
while ($x =~ /(\w+)/g) {
print "Word is $1, ends at position ", pos $x, "\n";
}
think you can modify $x while iterating .. or you could store pos $x in an array and remove then afterwards
我想你可以在迭代时修改$x。或者可以将pos $x存储在一个数组中,然后删除
#1
2
Here is a benchmark comparing regexp vs substr:
这里有一个对比regexp和substr的基准:
#!/usr/bin/perl
use 5.10.1;
use warnings;
use strict;
use Benchmark qw(:all);
my $count = -3;
my $r = cmpthese($count,
{
'substring' => sub {
my $string = "MATTHATBAT";
substr($string, 2, 1, '');
},
'regexp' => sub {
my $string = "MATTHATBAT";
$string =~ s/(.{2})./$1/;
},
}
);
Result:
结果:
Rate regexp substring
regexp 162340/s -- -93%
substring 2206122/s 1259% --
As you can see, substr is about 13.5 times as fast as regex.
可以看到,substr的速度是regex的13.5倍。
@Sinan Ünür 1259% is 13.5 times and not 12.5 times.
@Sinan Unur 1259%是13.5倍而不是12.5倍。
#2
1
Your example does work. The $string
will contain MATHATBAT
as you wanted to have, the problem is somewhere else, not in this part.
你的例子。$string将包含您想要的MATHATBAT,问题在其他地方,而不是这一部分。
#3
0
You can loop the reg.exp matches with //g
你可以循环这个reg。exp匹配/ / g
from perlrequick :
从perlrequick:
$x = "cat dog house"; # 3 words
while ($x =~ /(\w+)/g) {
print "Word is $1, ends at position ", pos $x, "\n";
}
think you can modify $x while iterating .. or you could store pos $x in an array and remove then afterwards
我想你可以在迭代时修改$x。或者可以将pos $x存储在一个数组中,然后删除