Tom,
If I have a string like " blah blah 7pm something
happens 8pm something else happens 9pm something
different"I want to use the times to split, and get the text
between the times.
...
string = " blah blah 7pm something happens 8pm
something else happens 9pm something different"
timepattern = /(\d{1,2})(:\d\d)?\s?([aApP]\.?[mM]\.?)/
...
If I use split, I can get everything, but in a format
that is useless to me (and to anybody, I'd guess).irb(main):006:0> string.split(timepattern)
=> [" blah blah ", "7", "pm", " something happens ",
"8", "pm", " something else happens ", "9", "pm",
" something different"]This gives me everything mixed together, but since
some capture groups are not there, you can't figure
out which part is regexp match, and which part is
text between regexps.
The reason that the capture groups are "mixed together" is that you
are controlling the grouping with your parenthesized expressions. If
you want grouping without affecting captures, use (?:re) instead of
(re):
irb(main):021:0> timepattern =
/(\d{1,2}(?::\d\d)?\s?(?:[aApP]\.?[mM]\.?))/
=> /(\d{1,2}(?::\d\d)?\s?(?:[aApP]\.?[mM]\.?))/
irb(main):022:0> string.split(timepattern)
=> [" blah blah ", "7pm", " something happens ", "8pm", " something else
happens ", "9pm", " something different"]
irb(main):023:0>
Does this get you any closer?
- Warren Brown