What is a Perl regex for finding the first non-consecutively-repeating character in a string.

Posted by DVK on Stack Overflow See other posts from Stack Overflow or by DVK
Published on 2010-03-30T21:13:32Z Indexed on 2010/03/30 21:23 UTC
Read the original article Hit count: 524

Filed under:
|

Your task, should you choose to accept it, is to write a Perl regular expression that for a given string, will return the first occurence of a character that is not consecutively duplicated. In other words, both preceded AND succeeded by characters different from itself (or start/end of string respectively).

Example:

IN: aabbcdecc
OUT: c

Please note that "not consecutively duplicated" does not mean "anywhere in the string".

NOTE: it must be a pure regex expression. E.g. the solution that obviously comes to mind (clone the string, delete all the duplicates, and print the first remaining character) does not count, although it solves the problem.

The question is inspired by my somewhat off-topic answer to this: http://stackoverflow.com/questions/2548606/perl-function-to-find-first-non-repeating-character-in-a-string

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex