Reputation: 24535
I want to remove all characters in a string except:
-
or _
or .
A
thru Z
a
thru z
0
to 9
On linux command line, using sed
I would do this:
$ echo "testing-#$% yes.no" | sed 's/[^-_.a-zA-Z0-9 ]//g'
Output:
testing- yes.no
How can I achieve the same effect in Red language with PARSE? I looked at:
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
Upvotes: 6
Views: 288
Reputation: 22560
An alternative solution to PARSE
would be to use REPLACE
here with a COMPLEMENT
CHARSET
:
replace/all "mystring%^&" complement charset [{-_. } #"a" - #"z" #"0" - #"9"] {}
NB. Above works in Rebol (2 & 3). Unfortunately it currently hangs in Red (tested on 0.63 on MacOS).
Upvotes: 2
Reputation: 3718
First note the difference between ISSUE! and CHAR!
#a #b #c ; issues
#"a" #"b" #"c" ; chars
You can then establish a character set (BITSET! type) either for the characters you want to keep or those you wish to discard. We'll do the former here:
good-chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]
Now that we have that, we can approach this in some different ways:
A fairly basic parse loop—skips any good-chars
and removes anything else.
parse "mystring%^&" [any [some good-chars | remove skip]]
Hopefully self-explanatory:
remove-each char "mystring%^&" [not find good-chars char]
Upvotes: 6
Reputation: 1301
First, characters must be in quotes, #a
is issue!
, char!
is #"a"
. You've got the specification right, but you must pass it to charset
function, to make a bitset!
form it.
Then you can parse
your string, keep
ing valid characters and skip
ing invalid:
>> chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]
== make bitset! #{000000000000FFC07FFFFFE07FFFFFE0}
>> rejoin parse "mystring%^&asdf" [collect some [keep chars | skip]]
== "mystringasdf"
Upvotes: 4