Regex that matches a pattern but does not match & ldquo ;: & rdquo;

advertisements

I have a regex that looks for certain types of hostnames like: .*-.*(nmtg)*|.*(\.nms). How do I modify this so it does not match: 11.22:33:44:55-66?

Should match:

cs25-admin.nmtg.company.com
cs25-admin

but should not match:

11.22:33:44:55-66


Two basic ways:

  • You can replace your "match anything" . with "match anything except for colon" [^:] everywhere

  • You can prepend your expression with "no colons from here to the end of string" (?!.*:)

EDIT As Signus said, your regexp is really non-specific and open-ended; it will match much more than what you think. For example, "----THRICEnmtgnmtgnmtg" is a full match, and so is "(-_-)". It is a better policy and easier to carefully specify what you want, rather than go listing exceptions. The regexps suggested by Signus are a good example.

They will still match within strings: "dont match this: example.com" will still match the "example.com" part. If that is what you want, cool. If not, you want to anchor the start and end of the string, by surrounding your regexp with /^.....$/.