Author Topic: help with wcmatch  (Read 4877 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
help with wcmatch
« 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

kpblc

  • Bull Frog
  • Posts: 396
Re: help with wcmatch
« Reply #1 on: August 22, 2013, 02:53:41 AM »
Do you mean
Code: [Select]
(setq str "*Abc, --")
(wcmatch str "`**")
?
Sorry for my English.

Coder

  • Swamp Rat
  • Posts: 827
Re: help with wcmatch
« Reply #2 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 ?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: help with wcmatch
« Reply #3 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 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:
Code: [Select]
"[*]"
« Last Edit: August 22, 2013, 04:33:52 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Coder

  • Swamp Rat
  • Posts: 827
Re: help with wcmatch
« Reply #4 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  :-(


ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: help with wcmatch
« Reply #5 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: help with wcmatch
« Reply #6 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.


Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: help with wcmatch
« Reply #7 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. ...

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: help with wcmatch
« Reply #8 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Coder

  • Swamp Rat
  • Posts: 827
Re: help with wcmatch
« Reply #9 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

lisp2learn

  • Guest
Re: help with wcmatch
« Reply #10 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

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: help with wcmatch
« Reply #11 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 :-)