-3

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\-]*)/
Paryeshakaya
  • 11
  • 1
  • 2
  • 5

2 Answers2

1

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 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 Oct 06 '09 at 08:29
  • @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 Oct 06 '09 at 08:47
  • 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. – Miserable Variable Oct 06 '09 at 08:50
  • @Greg: thanks for your answers. No, hosts like foo---bar.example.com or foo...bar.example.com should not match – Paryeshakaya Oct 06 '09 at 08:51
  • 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 Oct 06 '09 at 08:54
  • 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 Oct 06 '09 at 08:58
  • 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 Oct 06 '09 at 09:05
  • See my comments above, I should have solved. Thanks again for support – Paryeshakaya Oct 06 '09 at 09:46
  • Further note: I do not need to validate, but to match hostnames in an already existing log file – Paryeshakaya Oct 06 '09 at 09:47
0

In which language? The following should work in most

[a-z0-9-]+\([a-z0-9-.]+\)*
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • It should be used in a PHP script – Paryeshakaya Oct 06 '09 at 08:27
  • 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. – Miserable Variable Oct 06 '09 at 08:37