Reputation: 165
I have a regex rule for website urls. I'm not sure why the following is not working with my regex rule. Can someone help here?
Regex:
$regex = '/^(?:https?:\/\/(?:www\.)?)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?::[0-9]{1,5})?(\/.*)?$/';
Invalid URL format:
https://game.game123.io?r=235ffw12105gawfwara2332FWWF66e1EA9121685aa
Upvotes: 1
Views: 367
Reputation: 165
First of all thanks for all answers, the following 2 rules work, i just wonder which syntax is better since they seem to do the same?
regex 1
^(?:https?:\/\/(?:www\.)?)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?::[0-9]{1,5})?([?\/#].*)?$
regex 2
^(?:https?:\/\/(?:www\.)?)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?::[0-9]{1,5})?(?:[?\/#].*)?$
Is the additional ?: before [?/#] in syntax 2 required or not?
Upvotes: 1
Reputation: 163207
The pattern matches repetitions of either .
or -
followed by [a-z0-9]+
.
The full string does not match as there is a question mark in ?r=
, but the pattern expects a leading /
in the optional part at the end.
What you might do is match any of the character that you would allow using a character class [?/#]
in the last optional part followed by the rest of the line.
^(?:https?:\/\/(?:www\.)?)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?::[0-9]{1,5})?(?:[?/#].*)?$
^
Start of string(?:https?:\/\/(?:www\.)?)?
Optionally match the protocol and optional www.
[a-z0-9]+
Match 1+ a char a-z or digit 0-9(?:[-.][a-z0-9]+)*
Optionally repeat either -
or .
and match 1+ times a char a-z or a digit 0-9\.[a-z]{2,}
Match a .
and 2 or more times a char a-z(?::[0-9]{1,5})?
Optionally match :
and 1-5 digits 0-9(?:[?/#].*)?
Optionally match either ?
/
or #
$
End of stringUpvotes: 0