I'm looking for a way to split a string in Elixir without removing the pattern used to split, String.split/3 behaves like this
String.split "testZng", "Z"
# ["test", "ng"]
I'm looking for something like this
String.split "testZng", "Z"
# ["test", "Zng"]
Or like this
String.split "testZng", "Z"
# ["testZ", "ng"]
You may use lookarounds:
Regex.split ~r/(?=Z)/, "testZng"
# ["test", "Zng"]
Regex.split ~r/(?<=Z)/, "testZng"
# ["testZ", "ng"]
The (?=Z) is a positive lookahead that matches a location in the string that is immediately followed with a Z char.
The (?<=Z) is a positive lookbehind that matches a location in the string that is immediately preceded with Z.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With