Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way in C# to validate if a regular expression is well formed?

Tags:

c#

regex

Is there any simple way in C# to test if a regular expression is a regular expression? In other words, I would like to check if a user-provided regex pattern is malformed or not. This is purely a syntax test and not what the regex is supposed to achieve/test. Thanks

like image 617
Zoman Avatar asked Feb 06 '10 22:02

Zoman


People also ask

What is C programming in easy way?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

What is the easiest coding method?

If you're new to the world of programming, Python is the best coding language to start with. It's a beginner-friendly code with a simplified syntax, using fewer lines than other coding languages, making it easy to read, understand, and use.

Which C language is easy?

1. C++ C++, an extension of C—which we said was an easy language to learn—is a general-purpose programming language.


2 Answers

You may try passing it to the Regex constructor and catch potential ArgumentException which is thrown if the argument is a malformed regular expression.

like image 153
Darin Dimitrov Avatar answered Oct 23 '22 15:10

Darin Dimitrov


Here's an example from C# Online .NET that uses exceptions:

EDIT:

Removed the code to respect copyright owners, just in case. Simply click on the above link to see it.

like image 31
Andy West Avatar answered Oct 23 '22 14:10

Andy West