Want to encode text while calling Regex.Replace

I have a regex call that I need help with.

I did not post my regular expression, because here it is not relevant. What I want to have is during replacement, I also want to change part of $ {test} by doing Html.Encode in all the text that runs the regular expression.

Basically, wrap all the text that is within the regular expression with the bold tag, but also the HTML.Encode text between the bold tag.

RegexOptions regexOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase; text = Regex.Replace(text, regexBold, @"<b>${text}</b>", regexOptions); 
+4
source share
5 answers

Here is this implementation. I used to highlight special replacement strings from content and localization.

  protected string FindAndTranslateIn(string content) { return Regex.Replace(content, @"\{\^(.+?);(.+?)?}", new MatchEvaluator(TranslateHandler), RegexOptions.IgnoreCase); } public string TranslateHandler(Match m) { if (m.Success) { string key = m.Groups[1].Value; key = FindAndTranslateIn(key); string def = string.Empty; if (m.Groups.Count > 2) { def = m.Groups[2].Value; if(def.Length > 1) { def = FindAndTranslateIn(def); } } if (group == null) { return Translate(key, def); } else { return Translate(key, group, def); } } return string.Empty; } 

From the delegate of the conformity evaluator, you return everything you want to replace, so when I have the results you will have bold tags and a code call, mine also supports recursion, so it’s a little more complicated for your needs, but you can just hide the example for your needs .

This is equivalent to doing an iteration over a set of matches and doing parts of specifying replacement methods. It just saves you some code, and you can use the fancy shmancy delegate.

+2
source

There is an incredibly easy way to do this (in .net). It is called MatchEvaluator, and it allows you to do all kinds of cool searches and replacements. Essentially, you simply feed the Regex.Replace method to the method name, which returns a string and takes a Match object as the only parameter. Do whatever makes sense for your particular match (html encode), and the string you return will replace all the match text in the input string.

Example. Suppose you want to find all the places where two numbers are added (in the text), and you want to replace the expression with the actual number. You cannot do this with a strict regex approach, but you can, when you enter MatchEvaluator, it becomes easy.

 public void Stuff() { string pattern = @"(?<firstNumber>\d+)\s*(?<operator>[*+-/])\s*(?<secondNumber>\d+)"; string input = "something something 123 + 456 blah blah 100 - 55"; string output = Regex.Replace(input, pattern, MatchMath); //output will be "something something 579 blah blah 45" } private static string MatchMath(Match match) { try { double first = double.Parse(match.Groups["firstNumber"].Value); double second = double.Parse(match.Groups["secondNumber"].Value); switch (match.Groups["operator"].Value) { case "*": return (first * second).ToString(); case "+": return (first + second).ToString(); case "-": return (first - second).ToString(); case "/": return (first / second).ToString(); } } catch { } return "NaN"; } 

Learn more at http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx

+4
source

Do not use Regex.Replace in this case ... use ..

 foreach(Match in Regex.Matches(...)) { //do your stuff here } 
+3
source

If you execute the Regex.Match command, the resulting group of matching objects at the 0th index is a subset of the intput that matches the regular expression.

You can use this to stitch in bold tags and encode it there.

0
source

Can you fill in the code inside {} to add a bold tag and encode the text? I am confused about how to apply changes to the entire text block. And replace the section in the text variable at the end.

0
source

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


All Articles