JBalaguero
JBalaguero

Reputation: 201

Pattern for matching pieces of url

I have the following pattern: www.domain.com:8051/(.*)/(.*)

And the following two urls:

www.domain.com:8051/first/second

www.domain.com:8051/first/second/third/fourth

Both urls matches. But I only want to match the first one. What must I do to exclude the "/" as a character to match?

Thanks, Joan.

Upvotes: 0

Views: 85

Answers (3)

FThompson
FThompson

Reputation: 28687

You are using a greedy regex quantifier when you need to use the reluctant quantifier.

Rather than .*, use .*?. I found this cheat sheet extremely helpful when first learning how to use regular expression.

www.domain.com:8051/(.*?)/(.*?)

EDIT: After messing around with the pattern for a bit, I haven't been able to come up with a pattern which works, but you could check that the above pattern is found, and the following pattern is not: www.domain.com:8051/(.*?)/(.*?)/$

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160201

If those are the only patterns, why not just split on "/" and check the length?

You still have access to the components.

Upvotes: 1

uhz
uhz

Reputation: 2518

In order to exclude character you should use [^<characters to exclude>] so in your case:

www.domain.com:8051/([^/]*)/([^/]*)

Upvotes: 1

Related Questions