I have a string containing pairs of characters and I would like to replace each run by a single character. How can I do that?
我有一个字符串包含成对的字符,我想用一个字符替换每个字符。我怎么做呢?
This is a question from the official FAQ. We're importing the perlfaq to Stack Overflow.
这是官方FAQ里的一个问题。我们正在导入perlfaq到栈溢出。
3 个解决方案
#1
9
(This is the official perlfaq answer, minus any subsequent edits)
(这是官方的perlfaq回答,减去任何后续编辑)
You can use the substitution operator to find pairs of characters (or runs of characters) and replace them with a single instance. In this substitution, we find a character in (.)
. The memory parentheses store the matched character in the back-reference \g1
and we use that to require that the same thing immediately follow it. We replace that part of the string with the character in $1
.
您可以使用替换操作符查找字符对(或字符运行),并用单个实例替换它们。在这个替换中,我们在(.)中找到一个字符。内存圆括号将匹配的字符存储在后面的reference \g1中,我们使用它来要求立即执行相同的操作。我们将字符串的这一部分替换为$1中的字符。
s/(.)\g1/$1/g; # 5.10 or later
s/(.)\1/$1/g; # earlier versions
We can also use the transliteration operator, tr///
. In this example, the search list side of our tr///
contains nothing, but the c option complements that so it contains everything. The replacement list also contains nothing, so the transliteration is almost a no-op since it won't do any replacements (or more exactly, replace the character with itself). However, the s
option squashes duplicated and consecutive characters in the string so a character does not show up next to itself
我们还可以使用音译运算符tr//。在本例中,tr/// /的搜索列表端不包含任何内容,但是c选项补充了这一点,因此它包含所有内容。替换列表也不包含任何内容,因此音译几乎是不允许的,因为它不会做任何替换(或者更确切地说,用本身替换字符)。但是,s选项会在字符串中删除重复的和连续的字符,这样字符就不会出现在自己的旁边
my $str = 'Haarlem'; # in the Netherlands
$str =~ tr///cs; # Now Harlem, like in New York
#2
0
$str=~ s/(.)\1+/$1/g;
#3
0
This is the answer from perlfaq4 from the last stable release:
这是来自perlfaq4的回答,来自最后一个稳定版本:
How do I remove consecutive pairs of characters?
如何删除连续的字符对?
(contributed by brian d foy)
(布莱恩·d·福伊贡献)
You can use the substitution operator to find pairs of characters (or runs of characters) and replace them with a single instance. In this substitution, we find a character in (.). The memory parentheses store the matched character in the back-reference \1 and we use that to require that the same thing immediately follow it. We replace that part of the string with the character in $1.
您可以使用替换操作符查找字符对(或字符运行),并用单个实例替换它们。在这个替换中,我们在(.)中找到一个字符。内存圆括号将匹配的字符存储在backreference \1中,我们使用它来要求相同的东西立即跟随它。我们将字符串的这一部分替换为$1中的字符。
s/(.)\1/$1/g;
We can also use the transliteration operator, tr///. In this example, the search list side of our tr/// contains nothing, but the c option complements that so it contains everything. The replacement list also contains nothing, so the transliteration is almost a no-op since it won't do any replacements (or more exactly, replace the character with itself). However, the s option squashes duplicated and consecutive characters in the string so a character does not show up next to itself
我们还可以使用音译运算符tr//。在本例中,tr/// /的搜索列表端不包含任何内容,但是c选项补充了这一点,因此它包含所有内容。替换列表也不包含任何内容,因此音译几乎是不允许的,因为它不会做任何替换(或者更确切地说,用本身替换字符)。但是,s选项会在字符串中删除重复的和连续的字符,这样字符就不会出现在自己的旁边
my $str = 'Haarlem'; # in the Netherlands
$str =~ tr///cs; # Now Harlem, like in New York
#1
9
(This is the official perlfaq answer, minus any subsequent edits)
(这是官方的perlfaq回答,减去任何后续编辑)
You can use the substitution operator to find pairs of characters (or runs of characters) and replace them with a single instance. In this substitution, we find a character in (.)
. The memory parentheses store the matched character in the back-reference \g1
and we use that to require that the same thing immediately follow it. We replace that part of the string with the character in $1
.
您可以使用替换操作符查找字符对(或字符运行),并用单个实例替换它们。在这个替换中,我们在(.)中找到一个字符。内存圆括号将匹配的字符存储在后面的reference \g1中,我们使用它来要求立即执行相同的操作。我们将字符串的这一部分替换为$1中的字符。
s/(.)\g1/$1/g; # 5.10 or later
s/(.)\1/$1/g; # earlier versions
We can also use the transliteration operator, tr///
. In this example, the search list side of our tr///
contains nothing, but the c option complements that so it contains everything. The replacement list also contains nothing, so the transliteration is almost a no-op since it won't do any replacements (or more exactly, replace the character with itself). However, the s
option squashes duplicated and consecutive characters in the string so a character does not show up next to itself
我们还可以使用音译运算符tr//。在本例中,tr/// /的搜索列表端不包含任何内容,但是c选项补充了这一点,因此它包含所有内容。替换列表也不包含任何内容,因此音译几乎是不允许的,因为它不会做任何替换(或者更确切地说,用本身替换字符)。但是,s选项会在字符串中删除重复的和连续的字符,这样字符就不会出现在自己的旁边
my $str = 'Haarlem'; # in the Netherlands
$str =~ tr///cs; # Now Harlem, like in New York
#2
0
$str=~ s/(.)\1+/$1/g;
#3
0
This is the answer from perlfaq4 from the last stable release:
这是来自perlfaq4的回答,来自最后一个稳定版本:
How do I remove consecutive pairs of characters?
如何删除连续的字符对?
(contributed by brian d foy)
(布莱恩·d·福伊贡献)
You can use the substitution operator to find pairs of characters (or runs of characters) and replace them with a single instance. In this substitution, we find a character in (.). The memory parentheses store the matched character in the back-reference \1 and we use that to require that the same thing immediately follow it. We replace that part of the string with the character in $1.
您可以使用替换操作符查找字符对(或字符运行),并用单个实例替换它们。在这个替换中,我们在(.)中找到一个字符。内存圆括号将匹配的字符存储在backreference \1中,我们使用它来要求相同的东西立即跟随它。我们将字符串的这一部分替换为$1中的字符。
s/(.)\1/$1/g;
We can also use the transliteration operator, tr///. In this example, the search list side of our tr/// contains nothing, but the c option complements that so it contains everything. The replacement list also contains nothing, so the transliteration is almost a no-op since it won't do any replacements (or more exactly, replace the character with itself). However, the s option squashes duplicated and consecutive characters in the string so a character does not show up next to itself
我们还可以使用音译运算符tr//。在本例中,tr/// /的搜索列表端不包含任何内容,但是c选项补充了这一点,因此它包含所有内容。替换列表也不包含任何内容,因此音译几乎是不允许的,因为它不会做任何替换(或者更确切地说,用本身替换字符)。但是,s选项会在字符串中删除重复的和连续的字符,这样字符就不会出现在自己的旁边
my $str = 'Haarlem'; # in the Netherlands
$str =~ tr///cs; # Now Harlem, like in New York