Author Topic: Remove all characters from a string (refresh my memory)  (Read 4912 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 651
Remove all characters from a string (refresh my memory)
« on: August 30, 2014, 02:48:02 PM »
I already found a solution with loops, so this question is only to refresh my memory: Is there a function which removes all alphabetical characters from a string?

"32c" -> "32"
"123.5 cm" -> "123.5"
"- 12.3" -> "-12.3"
"x 28" -> "28"


I don't have much hope, because it should not remove "-".  :-o Maybe better to use my loop ..

EDIT: I edited now "characters" to "alphabetical characters".
« Last Edit: August 30, 2014, 04:45:06 PM by Peter2 »
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Remove all characters from a string (refresh my memory)
« Reply #1 on: August 30, 2014, 03:35:42 PM »
One way  :-)

Code - Auto/Visual Lisp: [Select]
  1. (vl-list->string (vl-remove-if '(lambda (i) (or (< 96 i 123) (< 64 i 91))) (vl-string->list "123.5 cm")))
  2.  

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #2 on: August 30, 2014, 04:27:07 PM »
Or:
Code - Auto/Visual Lisp: [Select]
  1. (defun remalph1 ( s )
  2.     (vl-list->string (vl-remove-if '(lambda ( x ) (or (= 32 x) (< 64 x 91))) (vl-string->list (strcase s))))
  3. )

Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun remalph2 ( s )
  2.     (vl-list->string
  3.         (vl-remove 32
  4.             (vl-string->list
  5.                 (vl-string-translate
  6.                     "abcdefghijklmnopqrstuvwxyz"
  7.                     "                          "
  8.                     (strcase s t)
  9.                 )
  10.             )
  11.         )
  12.     )
  13. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (remalph1 "123.5 cm")
  2. "123.5"
« Last Edit: August 30, 2014, 04:50:09 PM by Lee Mac »

Peter2

  • Swamp Rat
  • Posts: 651
Re: Remove all characters from a string (refresh my memory)
« Reply #3 on: August 30, 2014, 04:44:27 PM »
Good evening

sorry that I was not correct in my first post: I edited now "characters" to "alphabetical characters".

Thanks for the postings: Tharwats code is what I already found in other postings, and Lee was again able to compress the code to a minimum and to show alternatives.

Thanks again.
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #4 on: August 30, 2014, 04:46:02 PM »
 :-D

Code: [Select]
(defun remalph3 ( s )
    (if (wcmatch (strcase s t) "*[a-z ]*")
        (remalph
            (vl-string-subst "" " "
                (vl-string-subst "" "a"
                    (vl-string-subst "" "b"
                        (vl-string-subst "" "c"
                            (vl-string-subst "" "d"
                                (vl-string-subst "" "e"
                                    (vl-string-subst "" "f"
                                        (vl-string-subst "" "g"
                                            (vl-string-subst "" "h"
                                                (vl-string-subst "" "i"
                                                    (vl-string-subst "" "j"
                                                        (vl-string-subst "" "k"
                                                            (vl-string-subst "" "l"
                                                                (vl-string-subst "" "m"
                                                                    (vl-string-subst "" "n"
                                                                        (vl-string-subst "" "o"
                                                                            (vl-string-subst "" "p"
                                                                                (vl-string-subst "" "q"
                                                                                    (vl-string-subst "" "r"
                                                                                        (vl-string-subst "" "s"
                                                                                            (vl-string-subst "" "t"
                                                                                                (vl-string-subst "" "u"
                                                                                                    (vl-string-subst "" "v"
                                                                                                        (vl-string-subst "" "w"
                                                                                                            (vl-string-subst "" "x"
                                                                                                                (vl-string-subst "" "y"
                                                                                                                    (vl-string-subst "" "z" (strcase s t))
                                                                                                                )
                                                                                                            )
                                                                                                        )
                                                                                                    )
                                                                                                )
                                                                                            )
                                                                                        )
                                                                                    )
                                                                                )
                                                                            )
                                                                        )
                                                                    )
                                                                )
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
        s
    )
)
« Last Edit: August 30, 2014, 04:49:57 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #5 on: August 30, 2014, 04:50:55 PM »
Interestingly:

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq s "12afngr35 0481mnga19rngk0gna9ggnga02fnnlgnlav03998gah8gmgrjo39f8g4q8hg4008vn8fb84gaohf489ggkb498bgcm")
  2. "12afngr35 0481mnga19rngk0gna9ggnga02fnnlgnlav03998gah8gmgrjo39f8g4q8hg4008vn8fb84gaohf489ggkb498bgcm"
  3. _$ (strlen s)
  4. 100
  5. _$ (benchmark '((remalph1 s) (remalph2 s) (remalph3 s)))
  6. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  7.  
  8.     (REMALPH2 S).....1029 / 8.44 <fastest>
  9.     (REMALPH1 S).....3323 / 2.61
  10.     (REMALPH3 S).....8689 / 1.00 <slowest>

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Remove all characters from a string (refresh my memory)
« Reply #6 on: August 30, 2014, 05:56:40 PM »
in a simple case the easiest is:
Code: [Select]
(vl-string-trim "abcdefghijklmnopqrstuvwxyz " s)

pBe

  • Bull Frog
  • Posts: 402
Re: Remove all characters from a string (refresh my memory)
« Reply #7 on: August 30, 2014, 11:37:39 PM »
Vanilla ; As per OPs' requirements <remove alphabetical characters>
Code - Auto/Visual Lisp: [Select]
  1.   (Defun RemLetw (str c / a b c)   ;<-- While
  2.   (while (/= (Setq a (substr str 1 1)) "")
  3.     (if   (not (wcmatch (Strcase a)  "[A-Z],` "))
  4.       (setq c (strcat c a))
  5.     )
  6.     (setq str (substr str 2))
  7.   )
  8.   c
  9. )
  10.  
  11.  
  12. (Defun RemLetr (str c / a b c)       ;<-- repeat
  13.   (repeat  (strlen str)
  14.     (setq a (substr str 1 1)
  15.      b (substr str 2)
  16.     )
  17.     (if   (not (wcmatch (Strcase a)  "[A-Z],` "))
  18.       (setq c (strcat c a))
  19.     )
  20.     (setq str b)
  21.   )
  22.   c
  23. )
  24.  
  25.  
  26. (Defun RemLetwrc (str c)
  27.   (cond
  28.     ((= str "") c)
  29.     ((not (wcmatch (Strcase (Setq a (substr str 1 1))) "[A-Z],` "))
  30.      (setq c (strcat c a))
  31.      (RemLetwrc (substr str 2) c)
  32.     )
  33.     ((RemLetwrc (substr str 2) c)
  34.     )
  35.   )
  36. )

Second argument as an optional prefix

By changing the evaluated condition to

(wcmatch a "[0-9],`.");<--- "." some

it would be the other way around, leave all numeric characters < and then some >
« Last Edit: August 31, 2014, 03:52:23 AM by pBe »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Remove all characters from a string (refresh my memory)
« Reply #8 on: September 01, 2014, 06:09:59 AM »
My Vanilla version for this:
Code - Auto/Visual Lisp: [Select]
  1. (defun str-filter (source filter / n L s)
  2.   (setq n (strlen source))
  3.   (repeat n
  4.     (setq s (substr source n 1)
  5.           n (1- n))
  6.     (if (wcmatch s filter)
  7.       (setq L (cons s L))))
  8.   (apply 'strcat L))

It seems to work as expected for the OP's samples:
Code: [Select]
_$ (mapcar '(lambda (s) (cons s (str-filter s "[0-9],`-,`.")))
'("32c" "123.5 cm" "- 12.3" "x 28"))
(("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))

A further question though ... just to be sure ... what about scientific notation? Should an E/e be accepted if both preceding and succeeding characters are numbers?
« Last Edit: September 01, 2014, 06:14:43 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Remove all characters from a string (refresh my memory)
« Reply #9 on: September 01, 2014, 06:16:48 AM »
It seems to work as expected for the OP's samples:
Actually that wcmatch filter can be slightly reduced:
Code: [Select]
_$ (mapcar '(lambda (s) (cons s (str-filter s "[0-9.],-")))
'("32c" "123.5 cm" "- 12.3" "x 28"))
(("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #10 on: September 01, 2014, 06:46:34 AM »
It seems to work as expected for the OP's samples:
Actually that wcmatch filter can be slightly reduced:
Code: [Select]
_$ (mapcar '(lambda (s) (cons s (str-filter s "[0-9.],-")))
'("32c" "123.5 cm" "- 12.3" "x 28"))
(("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))

Or just:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '(lambda (s) (cons s (str-filter s "[0-9.-]")))
  2. '("32c" "123.5 cm" "- 12.3" "x 28"))
  3. (("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))

Or maybe:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '(lambda (s) (cons s (str-filter s "~[a-zA-Z ]")))
  2. '("32c" "123.5 cm" "- 12.3" "x 28"))
  3. (("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))
« Last Edit: September 01, 2014, 06:53:11 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #11 on: September 01, 2014, 06:58:04 AM »
Another, similar to those posted:

Code - Auto/Visual Lisp: [Select]
  1. (defun remalph4 ( s / c )
  2.     (cond
  3.         (   (zerop (setq c (ascii s))) "")
  4.         (   (or (= 32 c) (< 64 c 91) (< 96 c 123)) (remalph4 (substr s 2)))
  5.         (   (strcat (chr c) (remalph4 (substr s 2))))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '(lambda ( s ) (cons s (remalph4 s))) '("32c" "123.5 cm" "- 12.3" "x 28"))
  2. (("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove all characters from a string (refresh my memory)
« Reply #12 on: September 01, 2014, 07:00:05 AM »
in a simple case the easiest is:
Code: [Select]
(vl-string-trim "abcdefghijklmnopqrstuvwxyz " s)

Be careful with this method  ;-)
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-string-trim "abcdefghijklmnopqrstuvwxyz " "123abc456")
  2. "123abc456"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Remove all characters from a string (refresh my memory)
« Reply #13 on: September 01, 2014, 07:08:36 AM »
Be careful with this method  ;-)
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-string-trim "abcdefghijklmnopqrstuvwxyz " "123abc456")
  2. "123abc456"
Actually even one of the OP's samples don't work with that:
Code: [Select]
_$ (vl-string-trim "abcdefghijklmnopqrstuvwxyz " "- 12.3")
"- 12.3"

Or just:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '(lambda (s) (cons s (str-filter s "[0-9.-]")))
  2. '("32c" "123.5 cm" "- 12.3" "x 28"))
  3. (("32c" . "32") ("123.5 cm" . "123.5") ("- 12.3" . "-12.3") ("x 28" . "28"))
Awesome, thanks! Had an issue when I added the minus sign - prefixed it with a back-quote which then didn't work, so I left that on the outside of the wcmatch option group. Didn't realize that a dash is ignored if there's nothing following it.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Remove all characters from a string (refresh my memory)
« Reply #14 on: September 03, 2014, 04:42:39 AM »
Based on irneb's str-filter:
Code - Auto/Visual Lisp: [Select]
  1. ; Arguments:
  2. ; str:   String to filter.
  3. ; ref:   Reference string.
  4. ; keepP: T or nil.
  5. ;        T:   Keep the characters in str that are in ref and drop all other characters.
  6. ;        nil: Drop the characters in str that are in ref and keep all other characters.
  7. ; (StringFilter "- aaa 333 bbb 555" "-0123456789" T)   => "-333555"
  8. ; (StringFilter "- aaa 333 bbb 555" "-0123456789" nil) => " aaa  bbb "
  9. (defun StringFilter (str ref keepP)
  10.   (setq ref (vl-string->list ref))
  11.   (vl-list->string
  12.     (
  13.       (if keepP vl-remove-if-not vl-remove-if)
  14.       '(lambda (int) (vl-position int ref))
  15.       (vl-string->list str)
  16.     )
  17.   )
  18. )