Erlang Central

Difference between revisions of "Splitting A String"

From ErlangCentral Wiki

Line 27: Line 27:
[[Category:CookBook]][[Category::Regular Expressions]]
[[Category:CookBook]][[Category:Regular Expressions]]

Revision as of 01:41, 4 September 2006


You want to split a string based on some pattern, but you want the matches included.


The regexp-split and pregexp-split functions don't include the sections of the string that matched the regexp provided. Sometimes it's handy to be able to split a string into parts based on some regexp, but include the matches as well.

regexp_loop(Str, Parts, Index, []) ->
    lists:reverse([string:substr(Str, Index)] ++ Parts);
regexp_loop(Str, Parts, Index, Rem_Matches) ->
    {NextPt,PtLen} = hd(Rem_Matches),
    regexp_loop( Str, [ string:substr(Str, NextPt, PtLen),
                        string:substr(Str, Index, NextPt - Index)]
                      ++ Parts, NextPt + PtLen,
                      tl(Rem_Matches) ).

regexp_split_inclusive(Str, Regex) ->
    {match, Matches} = regexp:matches(Str, Regex),
    regexp_loop(Str, [], 1, Matches).
1> regexp_split_inclusive("How about a nice   hawaiian punch?" " +").
["How"," ","about"," ","a"," ","nice","   ","hawaiian"," ","punch"]