[erlang-questions] split

Jesper Louis Andersen <>
Fri Feb 14 11:04:58 CET 2014


Hi Joe,

I hereby accuse you of abusing regular expressions where less will do:

3> [P || P <- binary:split(<<"123abcabc456">>, <<"abc">>, [global]), P /=
<<>>].
[<<"123">>,<<"456">>]

In Standard ML, there are two token-split operations,

http://www.standardml.org/Basis/string.html#SIG:STRING.tokens:VAL

the important notion being that the 'tokens' variant omits redundant
whitespace (what you want) and 'fields' keeps the whitespace (what you want
when parsing e.g., CSV files). The code in e(3) probably doesn't generate a
lot of garbage.


On Fri, Feb 14, 2014 at 10:41 AM, Joe Armstrong <> wrote:

> I'm sitting here feeling very stupid, can anybody explain to me in terms
> that
> a child of five could understand what's going on here:
>
> Problem: I want to split a string on repeated  occurrences of the string
> "abc"
>
>   so split("123abcabc456") should return [<<"123">>,<<"456">>]
>
> So I thought I could use re for this:
>
> > re:split("123abcabc456", "(abc)+").
> [<<"123">>,<<"abc">>,<<"456">>]
>
>
> The manual page says the matching split string is not included in the
> output - at least that what it appear to me to say.
>
> How in the name of the great blue-eyed slimy thing that hides under stones
> can I get rid of the additional <<"abc">> - this is totally not obvious to
> me.
>
> try again:
>
> > re:split("123abcabcabc456", "abc",[notempty]).
> [<<"123">>,<<>>,<<>>,<<"456">>]
>
>
> I was under the vague impression that <<>> *was* and empty string
>
> I knew I hated regular expressions, can't they be outlawed?
>
>
>
> /Joe
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140214/deb69c37/attachment.html>


More information about the erlang-questions mailing list