Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 1020 Bytes

include-captures-with-string-split.md

File metadata and controls

33 lines (26 loc) · 1020 Bytes

Include Captures With String.split

The String.split/3 function comes with two options: trim and parts. However, when using it with a regex pattern, you gain access to a couple extra options, including include_captures. This is because when used with a regex pattern, String.split just invokes Regex.split which comes with extra options like include_captures.

Here is String.split in action by itself and with the supported trim option.

> String.split("23d", ~r/\d+/)
["", "d"]
> String.split("23d", ~r/\d+/, trim: true)
["d"]

Adding in the include_captures option, we get a resulting list that includes the value captured by the splitting regex.

> String.split("23d", ~r/\d+/, trim: true, include_captures: true)
["23", "d"]

This isn't a documented feature, so it is only supported as long as Regex.split supports it and as long as String.split continues to delegate to Regex.split.

h/t Chris Erin