-2

I am trying below code to find valid and not-valid names

string pattern = @"((?:[GE\-[RGrp]+))";
            foreach (var AzureResponse in Response)
            {

                if (AzureResponse.name!= null)
                {
                    Console.WriteLine("{0},{1} a valid resource name.", AzureResponse.name, Regex.IsMatch(@AzureResponse.name, pattern) ? "is" : "Is not");


                }

            }

But it is printing out all the resources names are valid even though i know few are not, it seems i am not able to get the correct regex.

What i want is:

  • any name which starts with "GE-RGrp" should be valid one and rest are not valid
shab
  • 989
  • 1
  • 15
  • 31
  • 1
    From the description you provide, wouldn't it be better to test for .StartsWith("GE-RGrp-") ? – spender Jan 23 '18 at 01:45
  • This `[GE\-[RGrp]` is a character class with chars `G`,`E`,`-`,`[`,`R`,`G`,`r`,`p` quantified with `+`. So any group of characters with those letters/symbols will be matched. If going the regex route better use something like `\bGE-RGrp(\w+)` or `\b(GE-RGrp\w+)` –  Jan 23 '18 at 02:42
  • 1
    Try `@AzureResponse.name.StartsWith("GE-RGrp")`. No need to use a regex if you check if a string starts with a literal substring. – Wiktor Stribiżew Jan 23 '18 at 07:46

1 Answers1

1

If you are set on using Regex, then string pattern = @"^GE-RGrp"; should do the trick.

Brett
  • 1,540
  • 9
  • 13