Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:18+00:00 2026-06-13T18:44:18+00:00

I need to verify that a string is in a certain format…here are the

  • 0

I need to verify that a string is in a certain format…here are the rules.

  • Can contain a colon and/or dot.
  • Both the colon and dot are optional
  • If a colon and/or dot is specified there must be at least one character to the left and one character to the right of the colon/dot.
  • The colon must be before the dot if both are specified
  • Only 0 or 1 colon and 0 or 1 dot is allowed
  • AnyString means a string of one or more unicode characters excluding colon and dot (colon and dot characters are not allowed as part of AnyString).

Examples:

Can be…

AnyString:AnyString.AnyString
AnyString:AnyString
AnyString.AnyString
AnyString

Cannot be…

AnyString:.AnyString
AnyString.AnyString:AnyString
AnyString:
AnyString.
:AnyString
.AnyString

I have tried lots of different combinations and I am just not good enough at Regular Expressions to get this one.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:44:19+00:00Added an answer on June 13, 2026 at 6:44 pm

    Well, that looks like:

    • It definitely starts with one or more non-colon-or-dot characters
    • It then optionally has a colon followed by one or more non-colon-or-dot characters
    • It then optionally has a dot followed by one or more non-colon-or-dot characters
    • If there are both “colon plus X” and “dot plus X” sections, the colon section must come first

    (Note that none of your now-edited-in explanation was present when I wrote the above, so it was just based on the examples.)

    So I’d expect that to be a regex like this:

    ^[^.:]+(?::[^.:]+)?(?:\.[^.:]+)?$
    

    Notes:

    • You’d want to put all of this in a verbatim string literal to avoid having to escape the backslashes, e.g.

      var regex = new Regex(@"^[^.:]+(?::[^.:]+)?(?:\.[^.:]+)?$");
      
    • ^ matches the start of a string

    • [^.:] will match any character other than dot or colon
    • + is the syntax for “at least one”
    • (?:<subexpression>) is the syntax for a non-capturing group
    • \. is an escaped dot, as . means “any character”
    • ? is the syntax for “zero or one” (i.e. optional)
    • $ matches the end of a string

    Test code:

    using System;
    using System.Text.RegularExpressions;
    
    class Test
    {
        static readonly Regex regex =
            new Regex(@"^[^.:]+(?::[^.:]+)?(?:\.[^.:]+)?$");
    
        static void Main()
        {
            AssertValid("AnyString:AnyString.AnyString",
                        "AnyString:AnyString",
                        "AnyString.AnyString",
                        "AnyString");
    
            AssertInvalid("AnyString:.AnyString",
                          "AnyString.AnyString:AnyString",
                          "AnyString:",
                          "AnyString:..Anystring",
                          "AnyString.",
                          ":AnyString",
                          ".AnyString");
        }
    
        static void AssertValid(params string[] inputs)
        {
            foreach (var input in inputs)
            {
                if (!regex.IsMatch(input))
                {
                    Console.WriteLine("Expected to match but didn't: {0}",
                                      input);
                }
            }
        }
    
        static void AssertInvalid(params string[] inputs)
        {
            foreach (var input in inputs)
            {
                if (regex.IsMatch(input))
                {
                    Console.WriteLine("Expected not to match but did: {0}",
                                      input);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have a string that I need to verify the correct format of;
I need a regex that will verify whether the input string is in format
I need to verify in php that a string should not start or end
I need to write a regular expression to verify that a string contains {
I need to verify using Selenium (or similar framework) that certain HTML content/items are
Given a string, I need to verify that the string contains the word $LOOP
I need some help getting the if statement to verify that the string Array
I need to verify that a code generated in one class is same as
I need to verify that a simplified DTD is really a subset of a
I have a type and an interface and I need to verify that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.