Posted By

hansamann on 02/02/07


Tagged

regex regular expressions groovyseries


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jsbournival
hendersk
mbanfi
adaptives


Groovy Series: Regular Expressions 3/3


 / Published in: Groovy
 

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

  1. //pattern examples. Creating a time pattern that can be reused.
  2. def timePattern = ~/\d\d:\d\d/
  3.  
  4. def timeString = "Between 06:00 and 10:00, most geeks are asleep."
  5. def matcher = timePattern.matcher(timeString)
  6. def times = []
  7. matcher.each { times << it }
  8. assert times.join(", ") == '06:00, 10:00'
  9.  
  10. def anotherTimeString = "But between 22:00 and 02:00, most geeks are awake"
  11. //we reuse the pattern
  12. matcher = timePattern.matcher(anotherTimeString)
  13. times = []
  14. matcher.each { times << it }
  15. assert times.join(", ") == '22:00, 02:00'

Report this snippet  

You need to login to post a comment.