TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CAB on April 06, 2009, 08:14:03 PM
-
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)
)
-
CAB,Nice work~
This is useful~
But:
(wcString "test-[abc]" "[*") ; --> nil
(wcString "test-[abc]" "*]") ; --> "test-[abc]"
-
some time ago I posted a function
http://www.theswamp.org/index.php?topic=27733.msg333229#msg333229
this one is a stripped version :)
(defun wcString_sub (String Pat)
(if (wcmatch String Pat)
String
(wcString_sub (substr String 1 (1- (strlen String))) Pat)
)
)
(defun wcString (String Pat)
(if (= String "")
nil
(if (wcmatch String (strcat Pat "*"))
(wcString_sub String Pat)
(wcString (substr String 2) Pat)
)
)
)
(wcString "VAV-1W-2" "1*2") ; --> "1W-2"
(wcString "VAV-1W-2" "-1*") ; --> "-1W-2"
(wcString "VAV-1W-2" "1W") ; --> "1W"
(wcString "VAV-1W-2" "#?") ; --> "1W"
-
(defun test (a b)
(cond
((wcmatch a (strcat "~*" b "*"))nil)
((and (wcmatch b "~`**") (wcmatch a (strcat "*?" b "*"))) (test (substr a 2) b))
((and (wcmatch b "~*`*") (wcmatch a (strcat b "?*"))) (test (substr a 1 (1- (strlen a))) b))
(t a)
)
)
-
CAB,Nice work~
This is useful~
But:
(wcString "test-[abc]" "[*") ; --> nil
(wcString "test-[abc]" "*]") ; --> "test-[abc]"
Try this:
(wcString "test-[abc]" "*`[")
-
Nice work fellas, I'll test them this morning. :)