integer filtering

integer filtering

sudarsann
Advocate Advocate
1,266 Views
6 Replies
Message 1 of 7

integer filtering

sudarsann
Advocate
Advocate

Hi

 

I have many strings in a drawing with alphabets, special characters and integers
Ex: MS 3256865-BP#G
I need only integers 3256865 in that strings. How can I remove the alphabets and special characters using lisp.

 

Regards
Sudarsan

0 Likes
Accepted solutions (1)
1,267 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

One way:

 

(setq str "MS 3256865-BP#G")

Command: (vl-string-trim "ABCDEFGHIJKLMNOPQRSTUVWXYZ -#" str)
"3256865"

 

I only included all upper-case letters and those punctuation characters used in your example, to be trimmed off the ends.  Include lower-case letters if needed, and any additional punctuation characters that might be used, or omit any letters and/or characters that never would be.

 

However, that won't remove any non-numeric characters from within the string of numbers, if any.  That would probably require something that steps through the string one character at a time, which can certainly be done if needed.

Kent Cooper, AIA
0 Likes
Message 3 of 7

sudarsann
Advocate
Advocate

Mr. Kent,

 

It is trimming ALPHABETS and some special characters only, but not trimming spaces and next line of Mtext and  "(double quotes). How can I trim these type of characters

 

Ex: MS24 72
120/6"S

 

This is the "MTEXT" I needs it as 2472120 how can I trim the space, next line and "(quotes).

Please advice me.

 

Regards

Sudarsan

0 Likes
Message 4 of 7

ВeekeeCZ
Consultant
Consultant

First, how about try THIS

 

Then you can thank to @martti.halminen, thanks Martti!

 

Spoiler
(defun c:GetNumUntil/ ( / stop)
  (vl-list->string
    (vl-remove-if-not '(lambda (x)
			 (if (= x 47)
			   (setq stop T))
			 (and (< 46 x 58)
			      (not stop)))
      (vl-string->list (cdr (assoc 1 (entget (car (entsel))))))))
  )

 

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

@sudarsann wrote:

.... 

It is trimming ALPHABETS and some special characters only, but not trimming spaces and next line of Mtext and  "(double quotes). How can I trim these type of characters

 

Ex: MS24 72
120/6"S

 

This is the "MTEXT" I needs it as 2472120 how can I trim the space, next line and "(quotes).

....


If your original question's example string had included non-numerical characters within the numerical ones, I wouldn't have suggested what I did, which assumes the numerical characters are all together as in your example, and takes other things off both ends of the string.  As mentioned, you can add more characters for it to remove in that way.  But if you need them removed from between numerical characters too, then BeekeeCZ's suggestion is one way to do it.

 

BUT by that standard, wouldn't you want it to return 24721206 from the example above?  If not, BeekeeCZ's suggestion will not do what you want -- how is a routine supposed to know that it should not keep the 6?

Kent Cooper, AIA
0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kent1Cooper wrote:

 

.... wouldn't you want it to return 24721206 from the example above?  If not, BeekeeCZ's suggestion will not do what you want -- how is a routine supposed to know that it should not keep the 6?


Yes, I guess maybe [I didn't test it] it will -- I hadn't looked closely enough to notice the "Until/" part.  But is that really a cut-off that you want?  Nothing about the example in your original question compares to that.

 

I also wonder about the operation you need.  That routine asks you to select an object, but only returns the adjusted string, and doesn't change the text content of the object.  If you have many objects, do you want to run a command separately on each one?  Or select many and have it run on all of them?  What do you want to do with the results?  Change the content of the selected objects?  Put them into new Text/Mtext, or into a Table?  Send them to an external file?  Etc., etc.

 

If you really do want all the numerical characters, here's the approach in Martti's routine, with the stop-at-/ aspect removed, as a function with a string argument, rather than a command with object selection, and returning the revised string to do with what you want:

 

(defun RNO (str); = Return Number(s) Only
  (vl-list->string
    (vl-remove-if-not
      '(lambda (x) (< 47 x 58))
      (vl-string->list str)
    ); vl-remove-if-not
  ); vl-list->string
); defun

 

Kent Cooper, AIA
0 Likes
Message 7 of 7

john.uhden
Mentor
Mentor
This might work...

(defun numbers-only (str / i char #)
(setq i 1 # "")
(repeat (strlen str)
(setq char (substr str i 1)
i (1+ i)
)
(if (wcmatch char "#")
(setq # (strcat # char))
)
)
#
)

John F. Uhden

0 Likes