String Split Groups

Call the function string-split-groups to split a string into a list of strings grouped by ranges of characters found consecutively in the original string. E.g. say you want the alphabetic and numeric characters extracted from a string like "AF023-BZ056" into a list like ("AF" "023" "-" "BZ" "056") for easier manipulations like incrementing.

Note: This function uses VisualLisp codes (if existing) which may not be available in your version of CAD. It has only been tested on full AutoCAD 2008-2012 running in Windows XP-32bit & Windows 7 - 64 bit. If your version of CAD does not contain the visual lisp functions required, then a less efficient (AutoLisp-only) method will be used to calculate the groups.


(string-split-groups <string> <groups>) --> (<grouping1> <grouping2> ... <groupingN>)

Returns a newly created list containing strings grouped by the specified ranges of characters.


Both these arguments are treated as by-value calls. Ensure you don't send quoted symbols as they might be altered inside the function.


Required value of the original string. It can be any of the following:

  • A direct coded string like "Test String".
  • A variable containing a string.
  • The result of a function call as a string value.


Required list containing wcmatch patterns for each group to split the string into. The pattern must be in a form matching a single character. Any characters found in the string, not matching any of the specified groups will be deemed to be in a default group. It can be any of the following:

  • A direct quoted list like '("@" "#").
  • A variable containing a list.
  • A list created from direct coded strings, string results from a function call and / or variables like (list "[- _]" digits (roman-numerals))
  • The result of a function call as a list of strings containing these groups.


A newly created single dimensional list containing strings extracted from the original string. Each are the consecutive characters matching into a single group.


(defun string-split-groups (string groups / temp char last-group result test-groups found)
  (if (and vl-position)
    (defun test-groups (char /) (vl-position t (mapcar (function (lambda (grp) (wcmatch char grp))) groups)))
    (defun test-groups (char / n)
      (setq n 0)
      (while (and (< n (length groups)) (not (wcmatch char (nth n groups)))) (setq n (1+ n)))
      (if (< n (length groups)) n)
  (if (> (strlen string) 0)
    (setq last-group (test-groups (setq char (substr string 1 1)))
          temp       char
          string     (substr string 2)
  (while (> (strlen string) 0)
    (if (/= last-group (setq found (test-groups (setq char (substr string 1 1)))))
      (setq result     (cons temp result)
            temp       ""
            last-group found
    (setq temp   (strcat temp char)
          string (substr string 2)
  (reverse (cons temp result))


Group all alphabetic and numerals separate from other characters.

(string-split-groups " B01 - B02" '("@" "#")) ; --> (" " "B" "01" " - " "B" "02")