1

I was wondering, is there any easy way for those who are really unfamiliar with regex to find out how to match a string with the regex format for the specified string?

For example, this string, which is generated from another function i have:

3A5AE0F4-EB22-434E-80C2-273E315CD1B0

I have no clue what so ever what the proper regex for this string is.

Lets say i had a function taking this string as a parameter

        public static bool isValidRegex(string inputString)
        {
            Regex myRegex = ????

            if inputstring matches myRegex then
            return true;

            else 
            return false;


        }

What would the Regex be to make this function return true?

NOTE: As i am unfamiliar with C# the code provided may not be correct at all, and i am aware.

1
  • Well, a string is not quite enough to construct a reliable regex. You need to define the rules. It seems your string uses hexadecimal digits, has 5 groups separated by -. Is the length of the groups fixed? Commented Jun 10, 2011 at 7:20

4 Answers 4

9

In this case, it looks like this is a Guid - if you're using .NET 4, the simplest approach is probably to use Guid.TryParse... but if you need to use a regex, it would probably be:

^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$

In other words, start of string, 8 hex digits, dash, 4 hex digits, dash, 4 hex digits, dash, 4 hex digits, dash, 12 hex digits, end of string. Note that I've assumed any alphabetic characters will be upper case rather than lower.

It's probably worth creating this just once, and possibly compiling it, so you'd end up with:

private static readonly Regex GuidPattern = new Regex
    ("^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$",
      RegexOptions.Compiled);

public static bool IsValidRegex(string inputString)
{
    return GuidPattern.IsMatch(inputString);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Whoa, thanks. This has to be the fastest and best answer i've had on stackoverflow. Thanks!
Note: On versions of .Net older than 4, you still test this without using your own Regex by using Guid's string constructor and checking for exceptions. Obviously TryParse is much cleaner.
1

if the format is 8 letters - 4 - 4 - 4 - 12

then it is

[0-9A-Z]{8}-[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{12}

Comments

0

Something like that:

^[0-9A-Z]{8}-[0-9A-Z]{4}[0-9A-Z]{4}[0-9A-Z]{4}[0-9A-Z]{12}$

Between the curly braces you have the number of the elements, and between the [] you have all possible numbers an letters.

Comments

0

Regex is a way of matching an exact value to a pattern. But you need to know what sort of pattern you will accept. In the example you quote what provides an acceptable match?

"3A5AE0F4-EB22-434E-80C2-273E315CD1B0" is the exact value

Would you be happy if someone passsed in "3A5AE0F4-EB22-434E-80C2-273E315CD1B1" or "xxxxxxxx-EB22-434E-80C2-273E315CD1B0"

or "xxx"

or 3.14257

etc.

So that's the first thing and it's determined by your requirements which you haven't made clear.

Then

 string sPattern = "^[a-zA-Z0-9]+$";
 bool match = Regex.IsMatch(yourstring, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

will return true if your string contains only alphanumeric characters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.