Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to catch strings with only 2 digits

Tags:

regex

I have searched for similar questions, and even though I found several answers, none seams to work. I have a list of strings

Z189
I142
M395
L210
V467
M203
Q277
Q461
Y440
S250
M162
Q96
22Q
W148
Q72
T22T
22TWE22
12E34

and I need a regex to find only strings holding only 2 digits (not more or less). So in this case:

Q96, 22Q, Q72, T22T

my failed attempts include:

\D\d{2}\D
(?<!\d)\d{2}(?!\d)
like image 400
Fierce82 Avatar asked Dec 12 '25 08:12

Fierce82


1 Answers

You could use ^\D*(\d)\D*(\d)\D*$

\D* makes sure anything before your first digit isn't another digit.

(\d) captures your first digit.

\D* makes sure there is no other digits between your two digits.

(\d) captures your second digit.

\D* makes sure anything after your second digit isn't another digit.

like image 106
Zenoo Avatar answered Dec 14 '25 04:12

Zenoo



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!