TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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 :-) ?
val = "*Abc, --"
(wcmatch val "*")
Many thanks
-
Do you mean
(setq str "*Abc, --")
(wcmatch str "`**")
?
-
Do you mean
(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 ?
-
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:
- # (a single numerical digit). So "#" will match "0", "1", "2" ... "9". "##" will match "00", "01", ... "99"
- @ (single alphabetical character). Similar to #, but matches A-Z and a-z.
- . (non alpha-numeric, single character). So that would be stuff like ,./<>?:";'[]\{}|`~!@#$%^&*()_+-= (and all the others which are not a-z,A-Z,0-9.
- * matches anything, including nothing, but also multiple "anythings". So "*" matches anything from "" through to any length of any string with any character you could place in there.
- ? matches anything, but only one "anything", cannot be a "nothing".
- ~ at the start does the same as if you did: (not (wcmatch ...)), except you can group it (see comma and the square brackets below)
- [] means any one of the contained characters. So "[AB2]" will match "A", "B" or "2".
- [~] means not contained. So "A[~BCD]" will NOT match "AB", "AC", "AD", but will match any other string starting with "A".
- - meant from this character to that character when used inside []. So rather than doing "[0123456789]", you can do the same using "[0-9]".
- , allows for 2 (or more) portions of a wcmatch. It's as if you added 2 wcmatches into an or. Something like this (or (wcmatch string "A*") (wcmatch string "B*")) can be rewritten like so (wcmatch string "A*,B*")
- ` 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.
Edit, formatting makes some issues. the "[ * ]" above should be like this:"[*]"
-
Thank you so much for the nice explanation as always irneb . :-)
Edit, formatting makes some issues. the "[ * ]" above should be like this:"[*]"
Unfortunately , this way did not work :-(
-
Irneb was right, he just pointed on * char.
Try this :
(wcmatch str "[*]*")
This could also be :
(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...
-
Good explanations Irné, kudos.
Edit, formatting makes some issues. the "[ * ]" above should be like this:"[*]"
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:
For me, the second option would be more appropriate as the square brackets are usually used to match a set of characters.
-
This could also be :
(wcmatch str ".*")
Be aware that this will match other symbols, not just an asterisk prefix:
-
Thank you so much for the nice explanation as always irneb . :-)
Edit, formatting makes some issues. the "[ * ]" above should be like this:"[*]"
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.
-
Thank you all for the great inputs and explanations .
I learned a lot from this thread . :-)
Many thanks
-
try this alternative:
(defun matchx (x)
(vl-some (function (lambda ( z ) (= 42 z)))(vl-string->list x))
)
Command: (matchx "The swamp")
nil
Command: (matchx "The* swamp")
T
Command: (matchx "*The swamp")
T
Command: (matchx "The Swamp*")
T
-
To test for an asterisk prefix, you could also use:
(defun asteriskprefix ( s ) (= 42 (ascii s)))
_$ (asteriskprefix "abc")
nil
_$ (asteriskprefix "*abc")
T
Welcome to the Swamp lisp2learn :-)