Regular Expressions Sample


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace RegularExpressions
  6. {
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. //-- Take input value and if it is in lastname, firstname format echoes firstname lastname to the console
  12. string userName = Console.ReadLine();
  13. Regex re = new Regex("(\\w+),\\s(\\w+)");
  14. userName = re.Replace(userName, "$2 $1");
  15. Console.WriteLine(userName);
  16.  
  17. //-- Take input value and if it is in mm/dd/yyyy format echoes yyyy/mm/dd to the console
  18. string curDate = Console.ReadLine();
  19. Regex reDate = new Regex("(\\d{2})/(\\d{2})/(\\d{4})");
  20. curDate = reDate.Replace(curDate, "$3/$1/$2");
  21. Console.WriteLine(curDate);
  22. }
  23. }
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.