tags:

views:

190

answers:

3

Regex that somewhat validates if a value has one of the following characteristics:

123-29-123-123.subdomain.zomg.com:8085
123.12.34.56:420

Unfortunately, I'm terrible at Regex, C#, google searches, and the differences between proper nouns and regular ones.

It can be a lose approximation, in fact I would go with anything that has a : colon separator with a port after it.

A: 
FrustratedWithFormsDesigner
You could use `[01]?\d{1,2}|2[01234]\d|25[012345]` instead of `\d{1,3}` to match only numbers from 0 to 255.
Jens
+1  A: 

Will this work?

^(?<Host>[^:]+)(?::(?<Port>\d+))?$

This gives me:

Host = 123-29-123-123.subdomain.zomg.com
Port = 8085

and

Host = 123.12.34.56:420
Port = 420
John Gietzen
That works until you try an IPv6 address
John Downey
Yeah, but at that point, the port format fails 100% of the time anyways.
John Gietzen
FrustratedWithFormsDesigner
@Frustrated: No, it isn't. Regexes are not parsers. Don't use regexes for parsing. Use regexes for pattern matching. The only way to tell if it is a valid host name is to try to use it as a host name. What's the regex to tell if a host is online?
John Gietzen
A: 

One can negate a match using what I call the Match Invalidator (?! ) (Match if Suffix is absent). (I wrote a blog article entited: Regular Expression (Regex) Match Invalidator (?!) in .Net on it)

In (?! Suffix) If the suffix is matched then the match fails. I have taken into account the possibilities of 256-999 and 000 using (?! ). Here is the pattern, use IgnorePatternWhiteSpace because I have commented the pattern:

string pattern = @"
^(                      # Anchor to the beginning of the line
                        # Invalidations
  (?!25[6-9])              # If 256, 257...259 are found STOP
  (?!2[6-9]\d)             # If 260-299 stop
  (?![3-9]\d\d)            # if 300-999 stop
  (?!000)                  # No zeros
  (\d{1,3})             # Between 1-3 numbers
  (?:[.-]?)             # Match but don't capture a . or -
 ){4,6}                 # IPV4 IPV6
(?<Url>[A-Za-z.\d]*?)   # Subdomain is mostly text
(?::)
(?<Port>\d+)";

HTH

OmegaMan
I think it is called negative lookahead (http://www.regular-expressions.info/lookaround.html)
João Portela