0

I have this few strings:

'user/1/myid_print'
'user/2/myid_print'
'user/3/myid_print'
'user/4/myid_print'
'user/5/myid_print'

The second part is the dynamic one which must contain only integers. What is it's regular expression?

1
  • 1
    Try this one: ^'user/\d*/myid_print'$ Commented Sep 17, 2015 at 1:45

2 Answers 2

2

Try this:

/user\/\d+\/myid_print/

the \d+ matches a number which contains at least one digit. if it is a non-zero number, replace the \d+ with [1-9]\d*

Sign up to request clarification or add additional context in comments.

2 Comments

It works, but why does we have to put / in the beginning and end of the expression?
@Aliyah, It is the default regex delimiter.
0

It depends a bit on what language you're using, but one valid answer for Python is:

user/\d/myid_print

The / character is often used in describing regular expressions and sometimes needs \ before it to make it match part of the string, a valid answer might be:

user\/\d\/myid_print

These match the text "user/" and "/myid_print" literally, and \d matches the pattern "any digit 0-9". If you need to match the numbers 1,2,3,4,5 only, then use [1-5] instead of \d

Test it here: https://regex101.com/r/mS4xN4/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.