Help with WCMATCH

Help with WCMATCH

msarqui
Collaborator Collaborator
1,299 Views
11 Replies
Message 1 of 12

Help with WCMATCH

msarqui
Collaborator
Collaborator

Hi guys,

 

May I have some help please?

I am trying to compare two variables with WCMATCH but without success.

 

tagstr= "{\\C256;This is a test with wcmatch\\Pinside an attribute multiline ####}"

oldstr= "####"

 

So, I did this but it is not working:

 

(wcmatch tagstr oldstr)

It always returns nil. What am I doing wrong?

 

 

Thanks

Marcelo

0 Likes
Accepted solutions (2)
1,300 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Accepted solution

The # is used to search for a single numeric character (example 0-9).

To search for the # character in a string you have to put a reverse quote in front of the #.

Also you have to have an * at beginning and end of oldstr to account for any other characters.

(setq tagstr "{\\C256;This is a test with wcmatch\\Pinside an attribute multiline ####}")
(setq oldstr "*`#`#`#`#*")
(wcmatch tagstr oldstr)

0 Likes
Message 3 of 12

msarqui
Collaborator
Collaborator
Many thanks mracad!
0 Likes
Message 4 of 12

msarqui
Collaborator
Collaborator
Last question:
I could also have ##### or ######### or ############## (more than four #) and this will also return True. So, Is there a way to limit to only four #?
0 Likes
Message 5 of 12

ВeekeeCZ
Consultant
Consultant

@msarqui wrote:
Last question:
I could also have ##### or ######### or ############## (more than four #) and this will also return True. So, Is there a way to limit to only four #?

Erase the surrounding asterisks.

 

(wcmatch "####" "`#`#`#`#")

0 Likes
Message 6 of 12

msarqui
Collaborator
Collaborator

Hi BeekeeCZ

 

(setq tagstr "{\\C256;This is a test with wcmatch\\Pinside an attribute multiline ####}")
(setq oldstr "`#`#`#`#")
(wcmatch tagstr oldstr) 

Returns nil (it must return T with four # and nil to any other quantity of #)

 

0 Likes
Message 7 of 12

ВeekeeCZ
Consultant
Consultant

if always is at least a single character around:

(wcmatch " #### " "*[~`#]`#`#`#`#[~`#]*")

or simply 

(wcmatch  " ####}" "* `#`#`#`#}*")

if the characters space and } are always around.

0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant

@msarqui wrote:
Last question:
I could also have ##### or ######### or ############## (more than four #) and this will also return True. So, Is there a way to limit to only four #?

If such Multiline Attributes are always in that same format, with the #### at the end and preceded by a space and followed by only a right brace, you can use the asterisk for whatever comes before [only], and tie down the rest to specifically that:

 

(setq oldstr "* `#`#`#`#}")

 

But if that sequence might fall anywhere within the content, I'm not sure it can be restricted to recognize only when there are exactly 4 of them.

Kent Cooper, AIA
0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

if always is at least a single character around:

(wcmatch " #### " "*[~`#]`#`#`#`#[~`#]*")
....


I tried that earlier and did something wrong with it, so I thought it didn't work, but when corrected, yes, it can be restricted to four of them, after all.  BUT you don't need the reverse-apostrophe ` escape character on wildcard characters when inside brackets [see Help]:

 

Command: (wcmatch "testing ####}" "*[~#]`#`#`#`#[~#]*")
T

 

whereas with more than four:

 

Command: (wcmatch "testing #####}" "*[~#]`#`#`#`#[~#]*")
nil

 

or fewer:

 

Command: (wcmatch "testing ###}" "*[~#]`#`#`#`#[~#]*")
nil

Kent Cooper, AIA
0 Likes
Message 10 of 12

marko_ribar
Advisor
Advisor
Accepted solution
(defun _wcmatch ( str char n / sub pos )
  (if (<= n (strlen str))
    (progn
      (setq sub "")
      (repeat n
        (setq sub (strcat char sub))
      )
      (if (setq pos (vl-string-search sub str))
        (if (not (vl-string-search char (substr str (+ 1 n pos))))
          t
        )
      )
    )
  )
)
Command: (_wcmatch "abcde#######sdf" "#" 7)
T

Command: (_wcmatch "abcde#######sdf" "#" 6)
nil

Command: (_wcmatch "abcde#######sdf" "#" 8)
nil

HTH, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 11 of 12

msarqui
Collaborator
Collaborator

Hi Kent,

 

Thanks for the reply. I start to understand the complexity of this because yes, that sequence might fall anywhere within the content, and the will not be aways there. I will do more tests with your solutions for a while.

 

Thanks,

Marcelo

0 Likes
Message 12 of 12

msarqui
Collaborator
Collaborator

Wow Marko, this was impressive!

Many thanks!

Marcelo

0 Likes