Regular expression to match "CP" followed by 3-5 digits

What would be the regex to accept "CP123" The first two letters are CP and the other 3 or 4 or 5 ns.

+1


a source to share


8 answers


CP[0-9]{3,5}

      



+11


a source


This will suit your requirements:

^CP\d{3,5}$

      

The ^ matches the beginning of a line, so it does not allow any characters to the left of "CP".

\ d matches a digit, {3,5} matches 3-5 digits.



$ matches the end of the line, so it doesn't allow characters to be added after numbers.

If you are using regex in a validation control, you can remove the ^ and $ as added by the control:

CP\d{3,5}

      

+6


a source


This should work for all regex engines:

CP[0-9]{3,5}

      

+4


a source


As per the update in the comments:

CP\d{1,5} 

      

if you want one to five digits after CP. Otherwise use

CP\d+ 

      

if you just want CP followed by at least one digit.

+2


a source


even if your question is not very clear, this should work:

r'^CP[0-9]{3,5}$'

      

+1


a source


Regex regxExp = new Regex( "CP[0-9]{3,5}" );
bool result = regxExp.IsMatch( //Expression );

      

0


a source


Thanks everyone

^ [Cc] [Pp] \ d {1,5} $

is the desired answer to my question.

Thanks for helping me.

0


a source


"$CP123[3-5]^"

      

-3


a source







All Articles