Read more

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

Arne Hartherz
December 17, 2020Software engineer at makandra GmbH

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"]
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2020-12-17 16:58)