fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. string input = "***XX*123456789~N3*123 E. Fake St. Apt# 456~N4*Beverly Hills*CA*902122405~REF*EI*902122405~HL*1*1*50*0~SBR*P*18*******MA~NM1*IL*1*Tom*Thompson*T***MI*123456789A~N3*456 W. False Ave.*Apt. #6B~N4*Beverly Hills*CA*90210~DMG*";
  9. string matchPattern = @"(~N3\*)(.*?)(~N4\*)";
  10. string replacePattern = "[^0-9a-zA-Z ]";
  11.  
  12. var res = Regex.Replace(input, matchPattern, m =>
  13. string.Format("{0}{1}{2}",
  14. m.Groups[1].Value,
  15. Regex.Replace(m.Groups[2].Value, replacePattern, " "),
  16. m.Groups[3].Value));
  17. Console.Write(res);
  18. }
  19. }
Success #stdin #stdout 0.04s 134720KB
stdin
Standard input is empty
stdout
***XX*123456789~N3*123 E  Fake St  Apt  456~N4*Beverly Hills*CA*902122405~REF*EI*902122405~HL*1*1*50*0~SBR*P*18*******MA~NM1*IL*1*Tom*Thompson*T***MI*123456789A~N3*456 W  False Ave  Apt   6B~N4*Beverly Hills*CA*90210~DMG*