Question

INPUT : There's string that numbers, and a string, dots and spaces. Notice that e defines a the separator between the numbers.

e.27.3.90.. .e 3.50 2.30..e2.0.1.2. .50..

OUTPUT : I want to remove all the spaces and those extra dots except for the one that makes up following and add a , before e,

,e273.90,e3502.30,e2012.50

PS: There are so many posts regarding regex in various kind. I tried to build one, but seems like no success so far.

  1. Please propose any efficient one shot regex or ideas.
  2. Would like to hear the performance gain/loss of this regex vs multiple replace()

Here is the code I have been gasping ;)..:

      List<string> myList;     
      string s = "";     
      string s2 = "";          
      string str = "e.27.3.90..bl% .e 3.50 2.30. #rp.e2.0.1.2..50..y*x";
      s = Regex.Replace(str, @"\b[a-df-z',\s]+", "");                               
      myList = new List<string>(Regex.Split(s, @"[e]"));
Was it helpful?

Solution

Last str is your result

     string str = "e.27.3.90..bl% .e 3.50 2.30. #rp.e2.0.1.2..50..y*x";
     str = Regex.Replace(str, "[^e^0-9]", "");
     str = Regex.Replace(str, "([0-9]{2}?)(e|$)", ".$1,$2");

     //str = "," + str.Substring(0, str.Length - 1);

OTHER TIPS

  1. Remove all dots from the string.
  2. Split the string into separate items at each "e".
  3. For each item, add a dot before the last 2 digits.
  4. Recombine the items back into one string, placing a comma between items.

These steps are easily performed with the standard String methods, but you could use regexes if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top