I am trying to write a regex which will validate a string.

The rules:
1) string can contain any character.
2) string cannot contain a : (colon) by itself.
3) string can contain :: (two colons) by themselves.
4) string cannot contain three or more colons by themselves.

Examples:
NO sip:userid@example.com
OK sip::userid@example status::code
OK ::
OK ::a::
NO :::bad_example::


I took the fist two rules and came up with ^:+. So, now the string will fail if there is any colons in it.
Rule 3 seems to be a bugger. I tried multiple variations of ::, .*, etc, but no joy.
My next steps will be passing over the string twice (once with rules 1 and 2, and once with rule 3) and if it works, I'll try rule 4.

Any help is welcomed.

P.S. I am not sure if there is a problem with moochers around here, but does asking for 'any help' mean that I want someone to write the code for me?
 

In reply to Regex Validation by jimb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.