How To Make The Last Part Of This Regex Optional?
I am looping through all the links on a page and matching their href values against the following pattern: ([^/]+)/([0-9]+)/([^/]+) Problem is there are 2 types of link formats on
Solution 1:
Put the last bit in brackets of a non-capturing group and add a ?
:
([^/]+)/([0-9]+)(?:/([^/]+))?
Solution 2:
Use ?
quantifier, which makes your pattern optional. It matches either 0 or 1 occurrence of the pattern.
Also, you need to group the last slash, with your last part of your regex, in a non-capturing group.
([^/]+)/([0-9]+)(?:/([^/]+))?
Solution 3:
If you add a ?
that should make the last part of the pattern optional.
Post a Comment for "How To Make The Last Part Of This Regex Optional?"