How can I condense standalone characters in Perl?

I am trying to identify and condense single (uppercase) characters in a string.

For instance:

"test AB test" → "test AB test"

"test ABC test" → "test ABC test"

"test AB test CDE test" → "test AB test CDE test"

It works for me for individual occurrences (as in the first example above), but cannot figure out how to link it for several occurrences.

$str =~ s/ ([A-Z]) ([A-Z]) / \1\2 /g;

I will probably feel stupid when I see a solution, but I'm ready for it. Thanks in advance.

+3
source share
2 answers
$str =~ s/\b([A-Z])\s+(?=[A-Z]\b)/$1/g;
+2
source

, , , . "A B C" "AB C", B - A.

s/([A-Z]) ([A-Z])/\1\2/g, , (, " " → "THISISATEST" ), .

, (, " IMA" → " IMA", "" ), , . , , , - (, " IMA" → " ^ M ^ A TEST" → " IMA TEST" ), , , , , .

+1

Source: https://habr.com/ru/post/1745845/


All Articles