-1

I've seen multiple solutions to remove non-ascii characters from a string using regex. I'd like to do the inverse, so removing ascii characters from a string so I am only left with the non-ascii ones.

Example:

without_ascii "a" #=> ""
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
jgozal
  • 1,480
  • 6
  • 22
  • 43

1 Answers1

1

Well, just do the opposite of answers you found!

"a".gsub(/\p{ASCII}/, '') #=> ""
"a".delete("\u{0000}-\u{007F}") #=> #=> ""

Note that the question you linked was using \P which means negation of \p for String#gsub. And ^a-z which means delete all except characters from a to z for String#delete.

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • thank you @Ulysse BN. Why does `"β".gsub!(/\p{ASCII}/, '')` return nil? as opposed to just the "β"? Using `.delete` works great, but I'd love to use the ascii posix of possible – jgozal Jun 23 '21 at 17:51
  • @jgozal because of [`gsub!`'s definition](https://devdocs.io/ruby~2.7/string#method-i-gsub-21): _Performs the substitutions of `String#gsub` in place, returning str, or nil if no substitutions were performed_ – Ulysse BN Jun 23 '21 at 17:54
  • but "β" is non-ascii right? shouldn't it have found something? – jgozal Jun 23 '21 at 17:59
  • Ah sorry I get it now. Right `"βf".gsub!(/\p{ASCII}/, '')` works – jgozal Jun 23 '21 at 18:00
  • is there anyway to use `.delete` with the ascii posix? – jgozal Jun 23 '21 at 18:01
  • try `s = "β"; s.gsub!(/\p{ASCII}/, ''); puts s` you'll see that it gives you a string without ascii! As for you second question, no, please consider reading https://ruby-doc.org/core-3.0.1/String.html – Ulysse BN Jun 23 '21 at 18:02
  • I'm done commenting, pretty please read [ruby documentation](https://ruby-doc.org/core-3.0.1/String.html) – Ulysse BN Jun 23 '21 at 18:09