Splitting A String
From Erlang Community
(Difference between revisions)
| Revision as of 00:18, 4 September 2006 (edit) Bfulgham (Talk | contribs) ← Previous diff |
Current revision (01:41, 4 September 2006) (edit) (undo) Bfulgham (Talk | contribs) |
||
| Line 27: | Line 27: | ||
| </code> | </code> | ||
| - | [[Category:CookBook]][[Category | + | [[Category:CookBook]][[Category:Regular Expressions]] |
Current revision
[edit] Problem
You want to split a string based on some pattern, but you want the matches included.
[edit] Solution
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"]
|

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

