How can I condense stand-alone characters in Perl?

Posted by brydgesk on Stack Overflow See other posts from Stack Overflow or by brydgesk
Published on 2010-05-17T18:04:27Z Indexed on 2010/05/18 0:20 UTC
Read the original article Hit count: 437

Filed under:
|

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

For example:

"test A B test" -> "test AB test"

"test A B C test" -> "test ABC test"

"test A B test C D E test" -> "test AB test CDE test"

I have it working for single occurrences (as in the first above example), but cannot figure out how to chain it for multiple occurrences.

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

I'll probably feel stupid when I see the solution, but I'm prepared for that. Thanks in advance.

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex