nelsonic
nelsonic

Reputation: 33164

How to match a url with specific pattern but not match very similar pattern

How do we write a regular expression that will match:

https://example.com/username/method

where the user is any alphanumeric username

but not:

https://example.com/method

where there is no username present but the method name could be a valid username
e.g: followers such that a person with username "followers" would have a list of followers viewable at: https://example.com/followers/followers (I know this is a bizarre example but its the edge case we are seeing!)

I searched StackOverflow and tried to write it: http://regexr.com/3bi27
but my RegEx Foo is not there (yet!)

Thanks in advance!

Upvotes: 0

Views: 67

Answers (1)

oruckdeschel
oruckdeschel

Reputation: 100

https://regex101.com/r/qY5pH6/4 Just take a look. I would post it as comment, but I have no rights to do that.

((https|http)://)?example.com/(.*)/(.+)

Would match:

But not: - https://example.com/method

Upvotes: 2

Related Questions