arrays - Split a string while keeping delimiters Haskell -


basically i'm trying split string [[string]] , concat results keeping delimiters in resultant list (even repeating in row).

something below kind of works, delimiter gets crunched 1 space instead of retaining 3 spaces

unwords . map (\x -> "|" ++ x ++"|") . words $ "foo   bar" -- "|foo| |bar|" 

ideally like:

"|foo||   ||bar|" -- or "|foo|   |bar|" 

i can't figure out how preserve delimiter, split functions i've seen have dropped delimiters resulting lists, can write 1 myself seems in standardish library , @ point i'm looking learn more basics includes getting familiar more colloquial ways of doing things.

i think i'm looking function like:

splitwithdelim :: char -> string -> [string] splitwithdelim "foo   bar" -- ["foo", " ", " ", " ", "bar"] 

or maybe it's best use regexes here?

you can split list, keeping delimiters using keepdelimsl , keepdelimsr functions in data.list.split package, here:

split (keepdelimsl $ oneof "xyz") "aazbxyzcxd" == ["aa","zb","x","y","zc","xd"] 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -