-1

How to find whether a string is a regular expression or normal string in C#. I find in java and PHP but not C#. Can anyone help me please. String testing = "Final";

I want to test whether "testing" is a regular expression or normal string.

Thankyou

Madhu
  • 29
  • 4

2 Answers2

2

you can try by exception handling

private static bool IsValidRegex(string input)
{ 
   try
   {
     Regex.Match("", input);
   }
   catch (ArgumentException)
   {
      return false;
   }

  return true;
}
Visakh V A
  • 331
  • 3
  • 19
1

You can evaulate string in Regex

private static bool IsValidRegex(string pattern)
{
    if (string.IsNullOrEmpty(pattern)) return false;

    try {
        Regex.Match("", pattern);
    }
    catch (ArgumentException){
        return false;
    }

    return true;
}

If method returns false you can accept this string as normal string.

Dakmaz
  • 61
  • 6