tags:

views:

79

answers:

3

Hi all

How can I match with regular expression hosts like 123-45-67-89.dummy37.ipx.northtelecom.net.uk or q6415-ipbffx02alas.tokyo.ocx.wa.jp?

Additional info, some samples (I modified some hosts for privacy reasons):

  • abc953.internetdsl.uwnet.pl
  • ip-200-200-200-200.static.puxdsl.pt.lu
  • 123-123-45-45.cost.xligtik.se
  • saul-wilhem-c-118-37.sewl-net.com
  • static-80-80-250-230.sdsl91.rokh.ny.premiernet.net
  • static-ip-cr73415187.kable.net.ca

etc.

Host that must NOT match: e.g., hosts like foo---bar.example.com or foo...bar.example.com

Solved: I should have solved with the following pattern:

/((?:[a-z0-9\-]*\.){1,}[a-z0-9\-]*)/
+1  A: 

A regular expression to match those hostnames might be:

[-.0-9a-z]+

However, that will probably match much more than you intend. You will have to be more specific about what exactly you want to match (and what you don't want to match).

Greg Hewgill
This will match .abcd, which is probably not a good thing
Hemal Pandya
That's correct, which is why I mentioned that the OP needs to be more precise about what *exactly* the regular expression should match.
Greg Hewgill
This one is really *too* terse. It even matches `....`
soulmerge
See my extra info above, I added other samples...
Paryeshakaya
@Paryeshakaya: That's great, but additional matching examples don't help define what you do want to match. Should the host `foo---bar.example.com` match? How about `foo...bar.example.com`? My point is that my answer is a *correct* answer to your question as stated, but it's almost completely useless. You will have to refine your question so that you are more precise about what you *do* and *don't* want to match with your regex.
Greg Hewgill
All of your examples would be matched by both mine and Greg's expressions. The problem is going to be that mine and to a greater extent Greg's will match strings you *don't* want matched. Did you follow the link in Arno's reply? smink's response there (http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address/106223#106223) looks really cool.
Hemal Pandya
@Greg: thanks for your answers. No, hosts like foo---bar.example.com or foo...bar.example.com should not match
Paryeshakaya
Then I don't know what rule you are using for determining what is a "valid" host name. The first example is a valid DNS hostname while the second is not.
Greg Hewgill
The hosts are parsed in log files (like /var/log/secure and /var/log/messages). What I showed in my samples is what exactly I meet, I have changed only few chars or IP for privacy reason. I know that some of them are not valid DNS hostnames.
Paryeshakaya
What I meant was that `foo---bar.example.com` is a valid DNS hostname, but `foo...bar.example.com` is not. This does not match your definition of what you want to accept and not accept, so I don't know what rule you are using to determine what is "valid". Without that information, nobody can create a regular expression for you until you can precisely state what you consider "valid".
Greg Hewgill
See my comments above, I should have solved. Thanks again for support
Paryeshakaya
Further note: I do not need to validate, but to match hostnames in an already existing log file
Paryeshakaya
A: 

In which language? The following should work in most

[a-z0-9-]+\([a-z0-9-.]+\)*
Hemal Pandya
It should be used in a PHP script
Paryeshakaya
I don't know PHP. You will need to check how grouping works, which I have used \( and \) for here. @arno's answer above http://stackoverflow.com/questions/1524266/regular-expression-for-host/1524281#1524281 is probably more useful than mine.
Hemal Pandya
A: 

Do you want to know generally how a regular expression for hostnames should look or do you have a specific problem with matching the two examples?

A discussion about regular expressions for hostnames you may find here: http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address

arno
@arno: thanks for suggestion, but I had seen the discussion you suggest but it did not help me
Paryeshakaya