1

Trying to combine two regexes from here to validate host names and IP addresses in a single statement.

I've got them working separately, but when combining using |, things get screwy, matching fragments of host names and ips, not whole valid patterns, which is not what I want. I need a single regex, using the host name and IP patterns below, to match either host or IP addresses.

I've provided scratch pads where I've tested each pattern; the last one is my attempt at combining them, with examples illustrating regex matching invalid fragments.

regex: Host Names

^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$

regex: IP Address

^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$

regex: Host name and IPs (not working)

^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$
Community
  • 1
  • 1
raffian
  • 31,267
  • 26
  • 103
  • 174
  • Well? Show us what you've got. Please include your regex in your question, not in some off-site resource. – Greg Hewgill Dec 20 '13 at 02:14
  • Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. I'd have expected someone with 4800 reputation to know this. –  Dec 20 '13 at 02:14
  • Combine them by `\(regex1\)\|\(regex2\)`. Backslashes may or may not be required depending on your language. – Guido Dec 20 '13 at 02:16
  • @GregHewgill I provided regex links to what I've been trying, the combined regex reflects my attempts, combining parentheses and using `|`, I'm not well versed with regex, my apologies if that was not enough. – raffian Dec 20 '13 at 02:23
  • @MikeW The regex patterns are quite long, so I chose to provide links to scratch pads where I've been testing them, all of which are working examples, illustrating the problem, so I'm not sure what you're ticked about. – raffian Dec 20 '13 at 02:31
  • 1
    @raffian You've provided links. If the links disappear, because you delete your samples or for some other reason, your question becomes meaningless. –  Dec 20 '13 at 02:32
  • @MikeW That's a good point, but SO has 1000's of questions with jsFiddle links and not much else; I'll update the question, nonetheless. – raffian Dec 20 '13 at 02:35

1 Answers1

2

You had a precedence issue. When you wrote

^(regex1)|(regex2)$

It was interpreted as

(^(regex1))|((regex2)$)

So any line starting with a valid hostname or ending in an IP address matched ok. A solution is to do

^regex1$|^regex2$

Which comes out as:

^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$|^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$
Guido
  • 2,571
  • 25
  • 37
  • +1 for the explanation, I was under the impression `^` and `$` to delimiters could not be used more than once. – raffian Dec 20 '13 at 02:40
  • Minor issue with this: the combined regex is matching `1`, `1.2`, and `1.2.3` IP patterns; I've been playing with it here, http://regex101.com/r/zC2eZ0, can't seem to figure it out. – raffian Dec 20 '13 at 03:39
  • 1
    @raffian, Actually it's matching them as hostnames. You should fix your hostname regex to not accept anything starting with a number. – Guido Dec 20 '13 at 15:36
  • simple change, http://regex101.com/r/mU6bE3, thx again – raffian Dec 20 '13 at 16:06