Posted By

hansamann on 02/02/07


Tagged

regex regular expressions groovyseries


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives
javadude


Groovy Series: Regular Expressions 1/3


 / Published in: Groovy
 

URL: http://hansamann.podspot.de/files/grails_podcast_episode_32_1036.mp3

This snippet gives you some interesting examples what you can do with Strings and GStrings in Groovy. Listen to the audio to get the best learning experience. Simply right-click on the title above -> Save as... to download the mp3 file of this part of the series.

The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk K├Ânig and Sven Haiges, further information about the topic of this episode can be found in the book

Groovy in Action - http://groovy.canoo.com/gina

  1. //regex patterns look strange, but groovy makes them look better
  2. //use slashy syntax to declare a pattern, get rid of double-esacping
  3. //example below both defines the same pattern to match a time like 15:01
  4. assert "\\d\\d:\\d\\d" == /\d\d:\d\d/
  5.  
  6. //find a string, return true if found, false otherwise
  7. assert 'The time is 15:01' =~ /\d\d:\d\d/
  8.  
  9. //note, you can easily use this for branching
  10. def timeString = 'It is now 17:34'
  11. def timeRegex = /\d\d:\d\d/
  12. if (timeString =~ timeRegex) assert true
  13.  
  14. //calling matcher.find() finds the first occurrence, then steps to the next if called again
  15. //notice we used a regex group in the pattern: /(\d\d:\d\d)/
  16. def matcher = ("It is 15:01, it is 15:02, it is 15:03" =~ /(\d\d:\d\d)/ )
  17. assert matcher instanceof java.util.regex.Matcher
  18. def times = []
  19. while (matcher.find())
  20. {
  21. times << matcher.group() //this adds each match to the list
  22. }
  23. assert times.join(",") == "15:01,15:02,15:03"
  24.  
  25. //a more groovy way to the same thing as above, eachMatch method in String
  26. //note: no grouping parentheses needed in regex! /\d\d:\d\d/
  27. times = []
  28. "It is 15:01, it is 15:02, it is 15:03".eachMatch(/\d\d:\d\d/) {
  29. times << it[0] //it is a list, position 0 contains the complete match
  30. }
  31. assert times.join(",") == "15:01,15:02,15:03"
  32.  
  33. //using groups with the matcher and each closure. Within the pattern, we can use GString replacements to make
  34. //the pattern readable
  35. def DATE = /\d\d.\d\d.\d\d\d\d/
  36. def TIME = /\d\d:\d\d/
  37. def dates = []
  38. ("Today is 31.01.2007, 15:01. Tomorrow is 01.02.2007, 15:01" =~ /($DATE), ($TIME)/).each { all, date, time ->
  39. dates << date
  40. }
  41. assert dates.size() == 2
  42. assert dates.join(', ') == '31.01.2007, 01.02.2007'

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adaptives on February 5, 2009

Hmmm interesting.

I changed line 41 to have one more group in the regex, and the code failed at runtime. Then when I added another parameter to the closure, it worked. The number of parameters expected in the closure are num_groups + 1. We need one more for the 'all' (which I guess must be holding all the groups of that match).

Thanks for the tutorial.

You need to login to post a comment.