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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)