0

I need to check using regex that a string matches this format:

http://site.com/users/1

e.g. a matching string is http://stackoverflow.com/users/587532 and http://stackoverflow.com/users/587532/umar should be not matching.

but I don't know the regex code to do this.

1

1 Answer 1

2

http://([^/]+)/users/(\d+)

The first capture group gives you the site's name, the second gives you the user number.

If you need to escape the / characters, just use \/:

http:\/\/([^\/]+)\/users\/(\d+)

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

3 Comments

/http:\/\/([^\/]+)\/users\/(\d+)$/g
I would add a ^ and $ to make sure you don't match a partial (your regex still matches the case where the '/umar' is behind the url, it just doens't include it in it's match.
@umar, the $ at the end of the regex makes the /g modifier superfluous.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.