0

Yep - another noob regex query, which I can't seem to get.

I'm trying to get all matches for the string foo.mydomain.com/ or foo.mydomain.com:1234/ or foo.mydomain.com:<random port>/ but any other paths do not match, ie. foo.mydomain.com/bar or foo.mydomain.com/bar/pewpew

I tried to use: foo.mydomain.com(.*)/$ (which starts with anything, then foo.mydomain.com, then any thing after that until a slash, then end. (This search query is anchored to the end of the line.)

But that doesn't work. It doesn't match when I pass in foo.mydomain.com:1234 but it correct says foo.mydomain.com/bar/pewpew is not a match (as expected).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • What about `foo.mydomain.com` (no trailing slash)--should it match? – Alan Moore Apr 13 '10 at 05:07
  • Because it's a good habit to get into... escape the '.'s in your domain. Also, it helps to verbalise what you're hoping to match, quite often the regex follows easily (if you can remember the syntax). In this case: match the domain, optionally followed by a port specifier, optionally followed by a slash – CurtainDog Apr 13 '10 at 05:15

2 Answers2

2

Try:

^foo\.mydomain\.com(?::\d+)?/?$
  • ^ : Start anchor
  • \. : . is a meta char..to match a literal . you need to escape it with \
  • (?:) : grouping
  • \d : A single digit
  • \d+ : one or more digits
  • ? : makes the previous pattern optional
  • $ : End anchor
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • Assuming PCRE - Perl-compatible regular expressions. Drop the '?:' and it is a POSIX ERE (extended regular expression). (Also, to match the port number without the trailing slash, you need '/?' before the '$'.) – Jonathan Leffler Apr 13 '10 at 05:06
  • @Purne.Krome: The regex would not match **foo.mydomain.com/bar**, are you sure checked it correctly ? – codaddict Apr 13 '10 at 05:24
  • yep i did. it was another route in my rewriter that is fraking things. I deleted my comment before u replied, hoping u wouldn't see it :P *blush* – Pure.Krome Apr 13 '10 at 05:37
0
foo\.mydomain\.com(:\d{1,5})?/\s*$

Try this one.

Gishu
  • 134,492
  • 47
  • 225
  • 308