Ruby: How to keep split delimiter (separate, or as part of substrings)

Posted Over 3 years ago. Visible to the public.

Ruby's String#split returns an array of substrings from the given string. Usually, this is missing the split characters:

>> 'user@example.com'.split('@')
=> ["user", "example.com"]

If you want to join those parts later on, you might know the split character and can just use it to join explicitly.
But if you split by a regular expression (for a set of split characters) that information is lost:

>> 'user@example.com'.split(/[@\.]/)
=> ["user", "example", "com"]

You can use a capture group to make those characters be part of the resulting array:

>> 'user@example.com'.split(/([@\.])/)
=> ["user", "@", "example", ".", "com"]

Or, you can use a capture group with look-behind to keep them with the resulting substrings:

>> 'user@example.com'.split(/(?<=[@\.])/)
=> ["user@", "example.", "com"]

An example use case for that is telling browsers to allow breaking a string after certain characters using wbr tags.

Arne Hartherz
Last edit
Almost 3 years ago
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2020-12-17 15:58)