Author Topic: ( Challange ) Return string from WCMATCH  (Read 3897 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10339
( Challange ) Return string from WCMATCH
« 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:

Code: [Select]
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;;  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$
)

Code: [Select]
(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)
)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

cjw

  • Newt
  • Posts: 162
Re: ( Challange ) Return string from WCMATCH
« Reply #1 on: April 07, 2009, 01:31:15 AM »
CAB,Nice work~

This is useful~

But:

(wcString "test-[abc]" "[*") ; --> nil
(wcString "test-[abc]" "*]") ; --> "test-[abc]"
« Last Edit: April 07, 2009, 01:35:21 AM by cjw »
The slow need to start early!

VovKa

  • Swamp Rat
  • Posts: 1099
  • Ukraine
Re: ( Challange ) Return string from WCMATCH
« Reply #2 on: April 07, 2009, 02:59:37 AM »
some time ago I posted a function
http://www.theswamp.org/index.php?topic=27733.msg333229#msg333229
this one is a stripped version :)
Code: [Select]
(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"
« Last Edit: April 07, 2009, 03:47:08 AM by VovKa »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: ( Challange ) Return string from WCMATCH
« Reply #3 on: April 07, 2009, 04:33:05 AM »
Code: [Select]
(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)
 )
)
« Last Edit: April 07, 2009, 04:42:32 AM by ElpanovEvgeniy »
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

CAB

  • Global Moderator
  • Seagull
  • Posts: 10339
Re: ( Challange ) Return string from WCMATCH
« Reply #4 on: April 07, 2009, 09:17:16 AM »
CAB,Nice work~

This is useful~

But:

(wcString "test-[abc]" "[*") ; --> nil
(wcString "test-[abc]" "*]") ; --> "test-[abc]"

Try this:
(wcString "test-[abc]" "*`[")
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10339
Re: ( Challange ) Return string from WCMATCH
« Reply #5 on: April 07, 2009, 09:20:59 AM »
Nice work fellas, I'll test them this morning. :)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.