0

I am trying to get the enclosed number between two slashes in a URL using regex. The code regex I have is not working, I am fairly new to regex and don't really understand it.

The regex:

http:\/\/?www\.?example\.com\/g\/(^\d$)\/\w

The URL:

http://www.example.com/g/1337/Game-Title

Trying to get the "1337", which is the PlaceId. The http:// and www. are optional

1
  • Which language are you using these regexes in? Commented Jun 22, 2015 at 8:05

1 Answer 1

5

Use \d+ to match one or more digits.

\b(?:http:\/\/)?(?:www\.)?example\.com\/g\/(\d+)\/\w

put http:// and www. inside a capturing or non-caturing group and then make it as optional by adding ? quantifier next to that group. For both http and https, it would be (?:https?:\/\/)?

DEMO

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

6 Comments

That got it partially working, but I want to to work without http;// OR www. being there.
In addition, the \w should probably be \w+ or [aA-zZ0-9_\-]+
@NielsAbildgaard he just want to capture that particular digit only, so no prob..
@AdamS. ya, both are optional.
^(?:http:\/\/)?(?:www\.)?example\.com\/g\/(\d+)\/[-\w]+$
|

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.