如何从 Perl 中的字符串中去除方括号
How to strip square brackets from string in Perl
我有一个变量,其值括在方括号中。
例如:我的 $status=[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]
现在,我想从上面的字符串中去掉方括号。我正在尝试使用正则表达式,但它不适用于方括号。
$status =~ s/[{}]//g; #This is working for curly braces
$status =~ s/[\[\]]//g; #is not working
你能帮忙吗
#!perl -w
use strict;
use warnings;
my $status="[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]";
$status =~ s/[\[\]]//g;
print "status is : $status\n";
A [
is not special inside a character class, unless it's the start of a POSIX character class (see "POSIX Character Classes" below). It normally does not need escaping.
A ]
is normally either the end of a POSIX character class (see "POSIX Character Classes" below), or it signals the end of the bracketed character class. If you want to include a ]
in the set of characters, you must generally escape it.
However, if the ]
is the first (or the second if the first character is a caret) character of a bracketed character class, it does not denote the end of the class (as you cannot have an empty class) and is considered part of the set of characters that can be matched without escaping.
所以...
$status =~ s/[][]//g;
然而,
$status =~ s/[\[\]]//g;
适合我。不确定为什么它不适合你,除非你的真实代码中有其他你没有显示的东西。
您也可以使用 tr
:
my $status = "[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]";
$status =~ tr/[]//d;
print "$status\n";
# Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS
我有一个变量,其值括在方括号中。 例如:我的 $status=[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]
现在,我想从上面的字符串中去掉方括号。我正在尝试使用正则表达式,但它不适用于方括号。
$status =~ s/[{}]//g; #This is working for curly braces
$status =~ s/[\[\]]//g; #is not working
你能帮忙吗
#!perl -w
use strict;
use warnings;
my $status="[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]";
$status =~ s/[\[\]]//g;
print "status is : $status\n";
A
[
is not special inside a character class, unless it's the start of a POSIX character class (see "POSIX Character Classes" below). It normally does not need escaping.
A
]
is normally either the end of a POSIX character class (see "POSIX Character Classes" below), or it signals the end of the bracketed character class. If you want to include a]
in the set of characters, you must generally escape it.
However, if the
]
is the first (or the second if the first character is a caret) character of a bracketed character class, it does not denote the end of the class (as you cannot have an empty class) and is considered part of the set of characters that can be matched without escaping.
所以...
$status =~ s/[][]//g;
然而,
$status =~ s/[\[\]]//g;
适合我。不确定为什么它不适合你,除非你的真实代码中有其他你没有显示的东西。
您也可以使用 tr
:
my $status = "[Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS]";
$status =~ tr/[]//d;
print "$status\n";
# Calculate version=SUCCESS, cleanup stale processes=SUCCESS, Update version=SUCCESS, mail=null, Post Actions=SUCCESS