chickadee » regex » string-split-fields

string-split-fields REGEXP STRING #!optional MODE STARTprocedure

Splits STRING into a list of fields according to MODE, where MODE can be the keyword #:infix (REGEXP matches field separator), the keyword #:suffix (REGEXP matches field terminator) or #t (REGEXP matches field), which is the default.

(define s "this is a string 1, 2, 3,")


(string-split-fields "\\w+" s)

  => ("this" "is" "a" "string" "1" "2" "3")


(string-split-fields "[^ ]+" s)

  => ("this" "is" "a" "string" "1," "2," "3,")

(string-split-fields " " s #:infix)

  => ("this" "is" "a" "string" "1," "2," "3,")

(string-split-fields "," s #:suffix)
 
  => ("this is a string 1" " 2" " 3")