TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on August 22, 2013, 02:47:09 AM

Title: help with wcmatch
Post by: Coder on August 22, 2013, 02:47:09 AM
Hello guys ,

How can I confirm if a string is containing of asterisk at the beginning or in the middle or at last sometimes  :-) ?

Quote
val = "*Abc, --"

Code: [Select]
(wcmatch val "*")

Many thanks
Title: Re: help with wcmatch
Post by: kpblc on August 22, 2013, 02:53:41 AM
Do you mean
Code: [Select]
(setq str "*Abc, --")
(wcmatch str "`**")
?
Title: Re: help with wcmatch
Post by: Coder on August 22, 2013, 03:11:35 AM
Do you mean
Code: [Select]
(setq str "*Abc, --")
(wcmatch str "`**")
?

That works nicely , thank you so much . :-)

Is there any more information about the way to use the function wcmatch other than the help document ?
Title: Re: help with wcmatch
Post by: irneb on August 22, 2013, 04:29:05 AM
Is there any more information about the way to use the function wcmatch other than the help document ?
Not that I know of, but AFAICT the help (http://docs.autodesk.com/ACD/2013/ITA/index.html?url=files/GUID-EC257AF7-72D4-4B38-99B6-9B09952A53AD.htm,topicNumber=d30e644314) should be sufficient.

I mean, it does state that the reverse quote ` escapes the character following it. I use it a lot to match a full-stop/period, since that is actually (any non-alpha-numeric character).

E.g. take that help page point by point:
Edit, formatting makes some issues. the "[ * ]" above should be like this:
Code: [Select]
"[*]"
Title: Re: help with wcmatch
Post by: Coder on August 22, 2013, 08:38:29 AM
Thank you so much for the nice explanation as always irneb . :-)

Edit, formatting makes some issues. the "[ * ]" above should be like this:
Code: [Select]
"[*]"

Unfortunately , this way did not work  :-(

Title: Re: help with wcmatch
Post by: ribarm on August 22, 2013, 08:48:28 AM
Irneb was right, he just pointed on * char.

Try this :

Code: [Select]
(wcmatch str "[*]*")

This could also be :

Code: [Select]
(wcmatch str ".*")

Note that with [ * ], you explicitly tell CAD that you want to match first * char... In the second example . means any of special characters among witch is also *... So first code is more precise; second * after specifying first char replaces all other string characters that follow...
Title: Re: help with wcmatch
Post by: Lee Mac on August 22, 2013, 08:49:16 AM
Good explanations Irné, kudos.

Edit, formatting makes some issues. the "[ * ]" above should be like this:
Code: [Select]
"[*]"
Unfortunately , this way did not work  :-(

Note that the pattern [*] will match the single asterisk character, since the square brackerts are matching a single character.
To match the asterisk as a prefix, you would use any of the following:

Code - Auto/Visual Lisp: [Select]
  1. _$ (wcmatch "*ABC" "[*]*")
  2. T
  3. _$ (wcmatch "*ABC" "`**")
  4. T

For me, the second option would be more appropriate as the square brackets are usually used to match a set of characters.

Title: Re: help with wcmatch
Post by: Lee Mac on August 22, 2013, 08:50:44 AM
This could also be :

Code: [Select]
(wcmatch str ".*")

Be aware that this will match other symbols, not just an asterisk prefix:

Code - Auto/Visual Lisp: [Select]
  1. _$ (wcmatch "?ABC" ".*")
  2. T
  3. _$ (wcmatch "/ABC" ".*")
  4. T
  5. _$ (wcmatch "(ABC" ".*")
  6. T
  7. ...
Title: Re: help with wcmatch
Post by: irneb on August 23, 2013, 01:35:02 AM
Thank you so much for the nice explanation as always irneb . :-)

Edit, formatting makes some issues. the "[ * ]" above should be like this:
Code: [Select]
"[*]"

Unfortunately , this way did not work  :-(
You're welcome.

Note that example is not specific to your OP. It was intended as an alternative to:
  • ` escape the next character. Say you wanted to check if the text contains any one of these special characters. You've got the option of placing them inside [], but then what about ~ and -? Much easier is to escape them. Thus you can get "`*" (or "[ * ]") will match "*" and only "*", while "*" will match anything.

What I might have missed in my explanations is that all those items are generally used together with each other. Usually using only one of them would result in tests for only one single character long strings: all except for * matches only single characters. So "`*" and "[ * ]" would only match "*", not "*Gobledy Gook". Thus as the others have explained you combine these to get something like "`**", so the "'*" tests if the string begins with * and then the "*" matches "anything" in the rest of the string. In effect every item you place in the pattern argument is as if you say "This position needs to match this", then the next item would be anded with the previous result.

Good explanations Irné, kudos.
...
For me, the second option would be more appropriate as the square brackets are usually used to match a set of characters.
Thanks, and I'm also in full agreement with the escaped asterisk instead of the alternatives. I just indicated it to show that [] also escapes most of those special characters implicitly.
Title: Re: help with wcmatch
Post by: Coder on August 23, 2013, 10:27:17 AM
Thank you all for the great inputs and explanations .

I learned a lot from this thread . :-)

Many thanks
Title: Re: help with wcmatch
Post by: lisp2learn on August 23, 2013, 01:06:55 PM
try this alternative:


Code: [Select]
(defun matchx (x)
(vl-some (function (lambda ( z ) (= 42 z)))(vl-string->list x))
)

Code: [Select]
Command: (matchx "The swamp")
nil
Command: (matchx "The* swamp")
T
Command: (matchx "*The swamp")
T
Command: (matchx "The Swamp*")
T
Title: Re: help with wcmatch
Post by: Lee Mac on August 24, 2013, 08:51:43 AM
To test for an asterisk prefix, you could also use:

Code: [Select]
(defun asteriskprefix ( s ) (= 42 (ascii s)))
Code: [Select]
_$ (asteriskprefix "abc")
nil
_$ (asteriskprefix "*abc")
T

Welcome to the Swamp lisp2learn :-)