Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx two equal characters in String

Tags:

java

regex

I'm starting with RegEx and I need help, I want to validate that there are two equal characters followed by A, or that they are separated (but remain the same) and in the middle A. I explain with examples:

BBA -> true
ABB -> true
BAB -> true
CCA -> true
ABC -> false
BAC -> false
BBBA -> false (there have to be only two)
ABBB -> false (there have to be only two)

At the moment I have something similar to this, but it does not work correctly:

(([B-Z])\1{2}A) | ([B-Z]{1}A[B-Z]{1}) | (A([B-Z])\1{2})

I know I'm not getting close to the correct answer, what I'm learning. If someone could give me a hand I would appreciate it very much.

like image 530
Gabriel Avatar asked Dec 06 '25 11:12

Gabriel


1 Answers

Use \b to match only words, and back references for each |.

\b([B-Z])\1A|([B-Z])A\2|A([B-Z])\3\b

Check: https://regexr.com/42bp0

like image 103
Rocky Li Avatar answered Dec 08 '25 01:12

Rocky Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!