I was working on a routine to remove a prefix & suffix from a string and wanted to use wild card characters.
As you know WCMATCH returns T/nil and I wanted a routine to return the matching string.
Like this:
(MatchString "342-WAT-2" "##2")
"342"
Maybe you have a better idea?
Also wanted a way to get the suffix part of a sting like this:
(MatchStringSuffix "342-WAT-2" "AT*")
"AT-2"
This is what I have so far:
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;; wcmatch String
;; return min string to match
;; else return nil for no match
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defun wcString (str pat / idx EndPtr w$)
(setq idx 0
EndPtr (strlen str)
)
(if (and (wcmatch str (strcat pat "*"))
(setq w$ ""))
(while
(cond
((> idx EndPtr) (setq idx nil)) ; exit
((wcmatch w$ pat) nil) ; exit
((setq idx (1+ idx)
w$ (substr str 1 idx))
)
)
)
)
w$
)
(defun c:test ()
(defun ReverseStr (str)
(vl-list->string(reverse (vl-string->list str)))
)
;; Look for a prefix sring
(print (wcString "VAV-1W-2" "*X")) ; --> nil
(print (wcString "VAV-1W-2" "*A")) ; --> "VA"
(print (wcString "VAV-1W-2" "*V")) ; --> "V"
(print (wcString "VAV-1W-2" "*2")) ; --> "VAV-1W-2"
(print (wcString "VAV-1W-2" "*1")) ; --> "VAV-1"
(print (wcString "VAV-1W-2" "??V")) ; --> "VAV"
;; Look for suffix
(if (/= (setq rs (wcString (ReverseStr "VAV-1W-2") (ReverseStr "W*"))) "")
(print (substr "VAV-1W-2" 1 (- 8 (strlen rs)))) ; --> "VAV-1"
)
(if (/= (setq rs(wcString (ReverseStr "VAVx1W-2") (ReverseStr "-2"))) "") ; should be "`-2"
(print (substr "VAVx1W-2" 1 (- 8 (strlen rs)))) ; --> "VAVx1W"
)
(if (/= (setq rs(wcString (ReverseStr "VAVx1W-2") (ReverseStr "1W-2"))) "") ; should be "1W`-2"
(print (substr "VAVx1W-2" 1 (- 8 (strlen rs)))) ; --> "VAVx"
)
(princ)
)