Should I create one complex RegEx or several or less complex?

Should I create one integrated RegEx to handle all the things at hand, or should I break one integrated RegEx into multiple Regex, which?

I am concerned about performance using sophisticated Regex. Will the complex Regex be broken into a smaller simple regular expression?

+6
source share
3 answers

I donโ€™t think that now there will be a big difference due to compiler optimization, however, using simple, you would easily understand your code, which, in turn, will simplify maintenance.

+2
source

If you want to give an exhaustive answer to a question about productivity, you need to check both cases.

As for readability / maintainability, you can write unreadable code in any language, and you can do regular expressions. If you're writing big, be sure to use the x modifier ( IgnorePatternWhitespace in C #) and use comments to create your regular expression.

A randomly selected example from one of my past answers in c# :

 MatchCollection result = Regex.Matches (testingString, @" (?<=\$) # Ensure there is a $ before the string [^|]* # Match any character that is not a | (?=\|) #Till a | is ahead " , RegexOptions.IgnorePatternWhitespace); 
+3
source

Complex regular expressions can be VERY slow, but it depends on your regular expression and your environment. Take the case of string.trim (). It can be trivially implemented using regular expressions. You can use one regex or two (remove the leading and trailing spaces separately). Here is someone who has taken 11 different javascript trim implementations and compared them in different browsers: http://blog.stevenlevithan.com/archives/faster-trim-javascript . In this case, one regular expression loses a lot of time in most situations.

-1
source

Source: https://habr.com/ru/post/910880/


All Articles