How to find the first repeated consecutive charater in string ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find the first repeated consecutive charater in string ?

Can we find it using regex ? If possible please tell regex pattern Example : '**:=1234561123' -> 1 '___commit___' -> m '1aabv2345568' -> a

9th Jan 2021, 5:15 AM
Ankeshwarapu Hitesh
Ankeshwarapu Hitesh - avatar
5 Answers
+ 3
In a regex pattern, whatever you put inside () will be a group. Also, you can match the xth group in a pattern by doing \x. For example, if I want the first and last characer to be the same in a string, I can use the pattern r"^(.).+\1
quot; The \1 will match only if the character is the same as the character in the 1st group. Similarly, \2 will match the second group. Here is a demonstration https://code.sololearn.com/c7XVdxNJTkR7/?ref=app These are all the hints you need to solve your problem
9th Jan 2021, 5:31 AM
XXX
XXX - avatar
+ 2
Ankeshwarapu Hitesh sorry, but if I tell you the exact pattern, then I would be solving your problem. I want you to solve your problem yourself. That is why I have given you all the hints as to what you need to solve it. If you didn't understand my explanation, let me know. I will link some sources. Happy coding!
9th Jan 2021, 7:09 AM
XXX
XXX - avatar
+ 1
I got it..🕺🕺 The pattern is r"([a-zA-Z0-9])\1"
9th Jan 2021, 8:09 AM
Ankeshwarapu Hitesh
Ankeshwarapu Hitesh - avatar
9th Jan 2021, 11:09 AM
XXX
XXX - avatar
0
XXX Thank you so much for answering my question.. Can u tell exact pattern for my examples ?
9th Jan 2021, 6:03 AM
Ankeshwarapu Hitesh
Ankeshwarapu Hitesh - avatar