Skip to content

Instantly share code, notes, and snippets.

@avit

avit/regexp.rb Secret

Created June 22, 2012 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avit/c5d28db4feedf01bc041 to your computer and use it in GitHub Desktop.
Save avit/c5d28db4feedf01bc041 to your computer and use it in GitHub Desktop.
Regexp: repeating capture group
pattern = %r{
\A
\s*(?:Every)?\s*
(?:
(?<monthweek>1st|2nd|3rd|4th|last|first|second|third|fourth)
(?:
\s*(?:and|&|,)\s*
(?<monthweek>1st|2nd|3rd|4th|last|first|second|third|fourth)
){0,3}
)?\s*
(?<weekday>(?:Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day)s?\s*
(?:of each month|of the month|every month)?\s*
\z
}xi
pattern.match "first and second and third monday"
# #<MatchData
# "first and second and third monday"
# monthweek:"first"
# monthweek:"third"
# weekday:"monday">
# What happened to monthweek: "second"?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment