How to uppercase regex pattern?

I am currently watching a project that makes great use of regular expressions. Input lines already have an uppercase and therefore the Regex IgnoreCase flag is set. The internal mechanism of MS RegEx, although then it changes the entire case back to the bottom, which is an unnecessary blow. Changing the uppercase regression pattern and removing the flag helps performance.

Does anyone know an algorithm library that can contain uppercase Reg ex patterns without affecting group names or escaped characters?

+6
source share
1 answer

You can go and look for lowercase letters that are not preceded by an odd number of backslashes:

(?<!(?<!\\)(?:\\\\)*\\)\p{Ll}+ 

Then pass the match to the MatchEvaluator , write it down and replace the text in the original line. I don't know C #, so this may not work right away (the code snippet is made and slightly modified from RegexBuddy ), but this is the start:

 string resultString = null; resultString = Regex.Replace(subjectString, @"(?<! # Negative lookbehind: (?<!\\)(?:\\\\)*\\ # Is there no odd number of backslashes | # nor \(\?<?\p{L}* # (?<tags or (?modifiers ) # before the current position? \p{Ll}+ # Then match one or more letters", new MatchEvaluator(ComputeReplacement), RegexOptions.IgnorePatternWhitespace); public String ComputeReplacement(Match m) { // You can vary the replacement text for each match on-the-fly return @"\0".ToUpper(); // or whatever is needed for uppercasing in .NET } 

Explanation:

 (?<! # assert that the string before the current position doesn't match: (?<!\\) # assert that we start at the first backslash in the series (?:\\\\)* # match an even number of backslashes \\ # match one backslash ) \p{Ll}+ # now match any sequence of lowercase letters 
+1
source

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


All Articles