Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See what pattern a Regex object was created with?

Tags:

c#

.net

regex

I have a Regex object created with the new Regex(string pattern) constructor, is there a way afterwards to see what pattern the regex object was created with?

I can't seem to be able to access the "pattern" string member in either the Regex or in RegexOptions objects.

Context: Reason I'm asking is I'm creating a few regex objects early on while initializing (patterns are stored in a config file), they then get passed to a different class to be used frequently. However, I also need to compare the pattern string to those stored in a SQL database at run-time.

I would prefer not having to pass a string with the pattern in addition to the regex object. I also feel that creating the object once at startup is not a bad idea since the regex will be reused hundreds of times?

Feel free to provide alternative advices.

like image 680
mtone Avatar asked Aug 19 '12 02:08

mtone


People also ask

Why * is used in regex?

* - means "0 or more instances of the preceding regex token"

What is regex object?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.


1 Answers

So, in the debugger hovering the cursor over a regex object was showing the pattern, so it had to be close. Turns out Regex.ToString() returns the pattern.

ToString: Returns the regular expression pattern that was passed into the Regex constructor.

like image 111
mtone Avatar answered Oct 05 '22 10:10

mtone