Jack S.
Jack S.

Reputation: 2803

Matching specific urls with regex

I want to check a string to make sure that it starts with http:// and is on the website example.com. I have this:

"^http://\\Bw{3}?\\.example\\.com/*"

Why doesn't it match?

Upvotes: 1

Views: 2550

Answers (4)

Andrew Hare
Andrew Hare

Reputation: 351698

Try this:

^https?://(?:www\.)?example\.com/?.*

Make sure you escape the backslashes when you use this as a string in Java:

"^https?://(?:www\\.)?example\\.com/?.*

Upvotes: 3

Paul
Paul

Reputation: 141917

Your \B is causing the problem. You're searching for a character between two characters, followed by three w's

IN fact it is impossible for anything to match this regex: /\Bw because you are searching for an empty string that is either between two non-word character, or between two word characters, but you've specified that it's between a non-word character on the left (/) and a word character on the right (w).

You also searching for 0 or more forward slashes at the end, instead of a forward slash followed by 0 or more wild card characters.

Try: ^http://(?:www\\.)?example\\.com(?:/.*)?

Upvotes: 0

sampwing
sampwing

Reputation: 1268

^http:\/\/(?:www\.)?example\.com(?:\/.*)?$

Upvotes: 0

Brandon
Brandon

Reputation: 4593

The regex for that is exactly what the address is...

"http://www.example.com" will match that and only that.

If you're looking to match other example.com strings such as

"http://test.example.com" the RegEx is something like this

http://\w+.example.com

Maybe I don't understand the question but that's the answer to what you've asked.

Upvotes: 0

Related Questions