[erlang-questions] newbie question

Fred Youhanaie fly@REDACTED
Sat Nov 15 18:31:59 CET 2014


I believe the first (empty) element of the returned list corresponds to the unmatched part at the start of the string, outside the sub-expression.

BTW, re:split/2,3 may return longer lists, if there are more sub-expressions in the pattern.

Cheers
Fred





On 15/11/14 14:07, kab wrote:
> resending
>
> I'm a noob trying to learn erlang too. I took Charles script and
> applied the feedback. The only part I'm not grokking is why re:split
> wants a 3 part list on the left hand side. I expected it to want two
> parts; one for the string matched and the second for the remaining
> string. Is there something about the re that is matching an empty
> string?
>
>
> -module(chunks).
>
> main(_) ->
>      io:format("Chunks Forward~n"),
>      CF = chunks_fwd("Able was I ere I saw Elba."),
>      lists:foreach(fun(A) -> io:format("~p~n", [A]) end, CF),
>      io:format("Chunks Reverse~n"),
>      CR = chunks_rev("Able was I ere I saw Elba."),
>      lists:foreach(fun(A) -> io:format("~p~n", [A]) end, CR)
>      .
>
> chunks_fwd("") -> [];
> chunks_fwd(Str)->
>      P1 = "^([[:alnum:]]+|[[:space:]]+|[[:punct:]]+|[[:cntrl:]]+|.)",
>      [[], M1, T1] = re:split(Str, P1, [{return, list}]),
>      [M1 | chunks_fwd(T1)]
>      .
>
> chunks_rev(Str) ->
>      chunks_rev(Str, []).
> chunks_rev("", Lst) -> Lst;
> chunks_rev(Str, Lst)->
>      P1 = "^([[:alnum:]]+|[[:space:]]+|[[:punct:]]+|[[:cntrl:]]+|.)",
>      [[], M1, T1] = re:split(Str, P1, [{return, list}]),
>      chunks_rev(T1, [M1|Lst])
>      .
>



More information about the erlang-questions mailing list