Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove special characters from string in Red language

Tags:

regex

rebol

red

I want to remove all characters in a string except:

  • - or _ or .
  • A thru Z
  • a thru z
  • 0 to 9
  • space

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:

  • http://www.rebol.com/docs/core23/rebolcore-15.html#section-1
  • http://rebol-land.blogspot.in/2013/03/rebols-answer-to-regex-parse-and-rebol.html
  • http://ross-gill.com/page/Beyond_Regular_Expressions

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
like image 529
rnso Avatar asked Sep 20 '17 18:09

rnso


People also ask

How do I remove special characters from a string in Ruby?

In Ruby, we can permanently delete characters from a string by using the string. delete method. It returns a new string with the specified characters removed.

How remove all special characters from a string in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).


2 Answers

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:

Parse

A fairly basic parse loop—skips any good-chars and removes anything else.

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

Remove-Each

Hopefully self-explanatory:

remove-each char "mystring%^&" [not find good-chars char]
like image 88
rgchris Avatar answered Sep 28 '22 01:09

rgchris


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, keeping valid characters and skiping invalid:

>> chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]
== make bitset! #{000000000000FFC07FFFFFE07FFFFFE0}
>> rejoin parse "mystring%^&asdf" [collect some [keep chars | skip]]
== "mystringasdf"
like image 34
rebolek Avatar answered Sep 28 '22 00:09

rebolek