nachoelchido
nachoelchido

Reputation: 139

How to split string in elixir keeping separators?

I have a String with special characters:

>> text = "ab()-cdef"

I want to separate the letters from the special characters in a list

>> list = ["ab", "(" , ")" ,"-", "cdef"]

I tried with

list = String.split(text,["(",")","-"], trim: false)

But I lose the special characters

list = ["ab","","","","cdef"]

Upvotes: 3

Views: 1655

Answers (3)

Oliver Ni
Oliver Ni

Reputation: 2664

The documentation for Regex.split mentions the :include_captures option:

iex> Regex.split(~r{(x)}, "Elixir", include_captures: true)
["Eli", "x", "ir"]

iex> Regex.split(~r{a(?<second>b)c}, "abc", on: [:second], include_captures: true)
["a", "b", "c"]

Note that when String.split is used with a regular expression you can use this option as well.

Upvotes: 2

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You were nearly there. Use lookarounds to split the string on non-letters.

String.split "ab()-cdef", ~r/(?<=\W)|(?=\W)/
#⇒ ["ab", "(", ")", "-", "cdef"]

Upvotes: 0

Michael Smith
Michael Smith

Reputation: 41

I used Regex.scan/2.

~r{\(|\)|-|[a-z]*}
|> Regex.scan("ab()-cdef")
|> List.flatten()
|> Enum.filter(fn s -> String.length(s) > 0 end)

I had to escape some special characters -- for example, ( became \( -- and separate special characters with pipes.

Upvotes: 1

Related Questions