Regular Expressions
-
Hello, can anyone help me in solving this issue. I have a string like "76/78NagdeviSt2ndFlrNagdeviM3" using regular expressions I would like to change it to "76/78 Nagdevi St 2nd Flr Nagdevi M3" Thanks in advance. Regards, Abraham
-
Hello, can anyone help me in solving this issue. I have a string like "76/78NagdeviSt2ndFlrNagdeviM3" using regular expressions I would like to change it to "76/78 Nagdevi St 2nd Flr Nagdevi M3" Thanks in advance. Regards, Abraham
First can I suggest you get hold of Expresso or one of the other tools that lets you play with Regexes so you get the correct one. I believe you are trying to insert a space in front of every upperCase character, so the simplest Regex is
([A-Z])
. Unfortunately however you cannot just Replace these as then you would end up with 76/78 agdevi t2nd lr agdevi 3Regex regex = new Regex("([A-Z])"); input = regex.Replace("76/78NagdeviSt2ndFlrNagdeviM3"," ");
Instead use a MatchEvaluator. This example demonstrates named targets, which is not the simplest way to achieve what you require, but does scale better with more complex Regexes.private string UpperEvaluator(Match match) { return " "+match.Groups["upper"].Value; } public string AddSpaces(string input) { Regex regex = new Regex("(?'upper'([A-Z]))"); return regex.Replace(input, new MatchEvaluator(this.HtmlStripEvaluator)); }