0

For some reason I always get stuck making anything past extremely basic regular expressions.

I'm trying to make a regular expression that kind of looks like a URL. I only want basic checking.

I would like it to match the following patterns where X is "something".

X://X.X

X://X.X... etc.

X.X

X.X... etc

If the string contains one of these patterns, it is sufficient checking for me. This way a url like www.example.com:8888 will still match. I have tried many different REGEX combinations with preg_match and cannot seem to get any to behave the way I want it to. I have consulted many other related REGEX questions on SO but my readings have not helped me.

Any help? I will be happy to provide more information if you would like but I don't know what else you would need.

2 Answers2

1

It takes practice but here is one that I made using a regex tester (http://www.regextester.com/) to check my pattern:

^.+(:\/\/|\.)([a-zA-Z0-9]+\.)+.+

My approach is to slowly build my pattern from the beginning and add on one piece at a time. This cheatsheet is extremely helpful for remembering http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ what everything is.

Basically the pattern starts at the beginning of the string and checks for any characters followed by either :// or . then checks for groupings of letters and numbers followed by a . ending with any number of characters.

The pattern could probably be improved with groupings to not pass on invalid characters. But this one was quick and dirty. You could replace the first and last . with the characters that would be valid.

UPDATE

Per the comments here is an updated pattern:

^.+?(:\/\/|\.)?([a-zA-Z0-9]+?\.)+.+
Schleis
  • 41,516
  • 7
  • 68
  • 87
  • The greediness of `+` is probably going to end up matching more than is required. – Kenneth K. Apr 09 '13 at 02:22
  • Thanks, but this actually doesn't work for something like `google.com` for me. Should it or is this likely my fault? –  Apr 09 '13 at 02:26
  • @Rell3oT, no it doesn't match google.com. And per the other comment there is some issues with greediness. – Schleis Apr 09 '13 at 02:29
  • Very strange. Your updated one doesn't match any of the ones I specified. Ill keep playing with it though –  Apr 09 '13 at 02:41
  • I fixed it. It was my fault. Thanks a lot Schleis I really appreciate it :D –  Apr 09 '13 at 03:01
0
/^(.+:\/\/)?[^.]+\.[^.\/]+([.\/][^.\/]+)*$/
Wicked
  • 165
  • 1
  • 6