Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to Match D or E followed by 2-3 digits

Tags:

regex

I've been looking for a regex to match a string that either starts with a D or an E followed 2 or 3 digits. I'm pretty terrible at writing regex but this is what I tried: ^[DE]{1}[0-9]{1,2}$

Thank you

like image 711
Whoppa Avatar asked Dec 12 '13 07:12

Whoppa


People also ask

Which regex matches one or more digits?

+: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.

What regex will find letters between two numbers?

You can use regex (. *\d)([A-Z])(\d. *) - This will give you exact ONE Alphabet between numbers.

How do I match a range of numbers in regex?

With regex you have a couple of options to match a digit. You can use a number from 0 to 9 to match a single choice. Or you can match a range of digits with a character group e.g. [4-9]. If the character group allows any digit (i.e. [0-9]), it can be replaced with a shorthand (\d).

How do I match a pattern in regex?

Matches pattern anywhere in the name. Marks the next character as either a special character, a literal, a back reference, or an octal escape. For example, 'd' matches the character "d". ' \d' matches a digit character.


1 Answers

starts with D or an E followed by 2 or 3 digits

You're close. Try this regex:

^[DE][0-9]{2,3}$

You don't need {1} as that is by default true and digits should be {2,3} instead of {1,2}

like image 170
anubhava Avatar answered Oct 07 '22 23:10

anubhava