LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   REGEX : detecting repetitive caracters (https://www.linuxquestions.org/questions/programming-9/regex-detecting-repetitive-caracters-4175621331/)

myre75 01-10-2018 03:29 AM

REGEX : detecting repetitive caracters
 
Hi all,

I need to detect, in Perl, a [0-9a-zA-Z_] where first repetetive characters will be find.

Ex :
abcd => NOK
aabcd => OK (aa)
abcdyyyyyy => OK (yyyyyy

thank you,

Regards,

syg00 01-10-2018 03:33 AM

Like you said, you need this.

So what effort have you made ?.

danielbmartin 01-10-2018 09:36 AM

Quote:

Originally Posted by myre75 (Post 5804524)
I need to detect, in Perl, a [0-9a-zA-Z_] where first repetetive characters will be find.

Ex :
abcd => NOK
aabcd => OK (aa)
abcdyyyyyy => OK (yyyyyy

You said you need to detect where the first repetitive characters will be found... yet your example simply indicates "found" or "not found." Please rethink and restate the problem.

Daniel B. Martin

.

pan64 01-10-2018 09:50 AM

you ought to check how regexp groups work and how can you specify repetition of a group.

MadeInGermany 01-10-2018 02:50 PM

Back-reference is the right term here. Repetition is misleading.
A repetition of the previous search pattern character:
Code:

[0-9a-zA-Z_]{2}
A back-reference needs a group, and it refers to what actually has been matched within the group.
Code:

([0-9a-zA-Z_])\1
Note: in perl the \1 remains available as $1 outside the regexp.
Code:

perl -lne '/([0-9a-zA-Z_])\1/ and print $1'

pan64 01-11-2018 01:26 AM

obviously, repetition was not used as a keyword, but was originally used by the OP (and was repeated). And also I did not want to give a full solution - because it is against the rules of LQ.


All times are GMT -5. The time now is 10:58 AM.