Removing special characters from a string in red

I want to delete all characters in a string except:

  • -or _or.
  • A thru Z
  • A thru Z
  • 0 to 9
  • space

On the linux command line, using sed, I would do the following:

$ echo "testing-#$% yes.no" | sed 's/[^-_.a-zA-Z0-9 ]//g'

Output:

testing- yes.no

How can I achieve the same effect in red with PARSE? I watched:

However, I could not codify it. I tried:

>> parse "mystring%^&" [#a - #z #A - #Z #0 - #9]
== false
>> parse "mystring%^&" [#a-#z#A-#Z#0-#9]        
== false
+4
source share
3 answers

-, , #a is issue!, char! is #"a". , charset, bitset!.

parse , keep skip ing :

>> chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]
== make bitset! #{000000000000FFC07FFFFFE07FFFFFE0}
>> rejoin parse "mystring%^&asdf" [collect some [keep chars | skip]]
== "mystringasdf"
+3

! CHAR!

#a #b #c  ; issues
#"a" #"b" #"c"   ; chars

( BITSET!) , , , . :

good-chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]

, , -:

- good-chars - .

parse "mystring%^&" [any [some good-chars | remove skip]]

Remove-

, :

remove-each char "mystring%^&" [not find good-chars char]
+5

PARSE REPLACE COMPLEMENT CHARSET:

replace/all "mystring%^&" complement charset [{-_. } #"a" - #"z" #"0" - #"9"] {}

NB. Above works in Rebola (2 and 3). Unfortunately, it currently hangs in Red (tested at 0.63 on MacOS).

+2
source

Source: https://habr.com/ru/post/1686059/


All Articles