Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Regex Named Capturing Groups in .NET - Regex Pattern
  2. string input =
  3.     "Mary Anne has been to 949 bingo games. The last was on Tue 24/04/2012. She won with the Numbers: 4, 6, 11, 16, 19, 27, 45";
  4.  
  5. string pattern =
  6.     @"(?<Person>w+?) has been to (?<NumberOfGames>d+?) bingo games. The last was on (?<Day>...?) (?<Date>...?). She won with the Numbers: (?<Numbers>...?)";
  7.  
  8. Regex regex = new Regex(pattern);
  9. var match = regex.Match(input);
  10.  
  11. string person = match.Groups["Person"].Value;
  12. string noOfGames = match.Groups["NumberOfGames"].Value;
  13. string day = match.Groups["Day"].Value;
  14. string date = match.Groups["Date"].Value;
  15. string numbers = match.Groups["Numbers"].Value;
  16.        
  17. string pattern = @"(?<Person>[w ]+) has been to (?<NumberOfGames>d+) bingo games. The last was on (?<Day>w+) (?<Date>dd/dd/d{4}). She won with the Numbers: (?<Numbers>.*?)$";
  18.        
  19. string title = match.Groups["Person"].Value;
  20. string drawNumber = match.Groups["NumberOfGames"].Value;
  21.        
  22. string title = match.Groups["Person"].Value;
  23. string drawNumber = match.Groups["NumberOfGames"].Value;
  24.        
  25. string pattern = @"(?<Person>w+?) has been to (?<NumberOfGames>d+?) bingo games. The last was on (?<Day>...?) (?<Date>d+/d+/d+). She won with the Numbers: (?<Numbers>...?)";