tags:

views:

62

answers:

2

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).

Any ideas?

+2  A: 

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
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
@Jonathan: Thanks for pointing :)
codaddict
@Purne.Krome: The regex would not match **foo.mydomain.com/bar**, are you sure checked it correctly ?
codaddict
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
A: 
foo\.mydomain\.com(:\d{1,5})?/\s*$

Try this one.

Gishu
the port can be more than 4 chars....
Pure.Krome
@Pure. : oops. 0 - 65535. Losing my mind... one digit at a time. Fixed. Thanks!
Gishu