What is a Perl regular expression to find the first non-repeating repeated character in a string?

Your task, if you decide to accept it, is to write a Perl regular expression that, for a given string, will return the first occurrence of a character that is not duplicated sequentially. In other words, both are preceded by AND, which are preceded by characters other than itself (or the beginning / end of a line, respectively).

Example:

IN: aabbcdecc
OUT: c

Note that “not being sequentially duplicated” does not mean “anywhere on the line”.

NOTE: this must be a pure regex expression. For instance. a solution that obviously comes to mind (clone a line, delete all duplicates and print the first remaining character) is not considered, although it solves the problem.

The question is caused by my somewhat unanswered answer to this: How can I find the first non-repeating character in a string using Perl?

+3
source share
3 answers
(?:(.)\1+)*(.?)

Get a second grab. (Will return an empty string if each character is duplicated sequentially.)

Test cases:

~:2434$ perl -e "\"abc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
a
~:2435$ perl -e "\"aabbcc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"

~:2436$ perl -e "\"aabbc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
c
~:2437$ perl -e "\"aabcc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
b
~:2438$ perl -e "\"aabcbbbcccccc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
b
~:2439$ perl -e "\"aabbvbbcccccc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
v
~:2440$ perl -e "\"aabbcdecc\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
c
~:2441$ perl -e "\"aabbccddeef\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
f
~:2442$ perl -e "\"faabbccddeef\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
f
~:2443$ perl -e "\"faabbccddeefax\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
f
~:2444$ perl -e "\"xfaabbccddeefx\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
x
~:2445$ perl -e "\"xabcdefghai\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
x
~:2446$ perl -e "\"cccdddeeea12345\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
a
~:2447$ perl -e "\"1234a5678a23\" =~ m/(?:(.)\1+)*(.?)/; print \$2;"
1

Or (will not match if each character is duplicated sequentially.)

(?:^|(.)(?!\1))(.)(?!\2)
+2
source
use 5.010;
$str=~/^(([a-z])\g{-1}+)*(?<c>[a-z])/i;
$char = $+{c};
+1
source

, Perl regex negate! .. , /regex/

, , regex capture:

m/(.)(\1)+/

( ). .

:

(?:^|(.)(?!\1))(.)(?!\2) 

"f" 2 3. "f" 2 3 5.

:

$str=~/^(([a-z])\g{-1}+)*(?<c>[a-z])/i;
$char = $+{c};

.

, , :

#!/usr/bin/perl
while( <DATA> ) {
    chomp;
    print "BEFORE: $_\n";
    s/(.)(\1)+//g;
    print "AFTER: $_\n";
    print "charater: " . substr($_,0,1) . "\n\n";
 }
__END__
aabbccddeef
faabbccddeef
faabbccddeefax
xfaabbccddeefx
xabcdefghai
cccdddeeea12345
1234a5678a23
aabbcdecc
abcdefg
aabbccddeef
cccdddeeea12345

" ". ((edit: reread: , , , ...))

, .

0

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


All Articles