Split and Maintain Delimiter
-
21-12-2019 - |
Question
September 1997, such and such
I need to split the above text into
September 1997
such and such
The delimiter is 1997, that is
/[0-9][0-9][0-9][0-9]\,/
How can I do it in ruby via regex?
That's my attempt:
"September 1997, such and such".to_s.split(/\s+(?=[0-9][0-9][0-9][0-9]\,)/)
Output:
September
1997, such and such
What I want is:
September 1997
such and such
Solution
Splitting September 1997, such and such
like this
(?<=\d{4}),\s*
should get this output
September 1997
such and such
You could even add extra whitespace
(?<=\d{4})\s*,\s*
for variability and trimming.
OTHER TIPS
Note this only splits once, but that seems to be what you want?
str = "September 1997, such and such"
p str.match(/(.*?)([0-9]{4}.*)/)
# => #<MatchData "September 1997, such and such" 1:"September " 2:"1997, such and such">
This does do multiple splits (edited to include \b
thanks to p11y)
p str.split(/(?=\b\d{4}\b)/)
# => ["September ", "1997, such and such"]
Using positive lookahead, you can split at the empty string, followed by a decimal (\d
). In this case that means that the empty string must be followed by a decimal, but the decimal will not be part of the split.
"September 1997, such and such".split(/(?=\d)/)
#=> ["September ", "1997, such and such"]
If you need to match exactly four decimals you can use a quantifier ({n}
), e.g.
"September 1997, such and such".split(/(?=\d{4})/)
#=> ["September ", "1997, such and such"]
If you want to get rid of the space in between, you can additionally capture one or more whitespace characters (\s+
) instead of the empty string:
"September 1997, such and such".split(/\s+(?=\d)/)
#=> ["September", "1997, such and such"]