LoginSignup
24
23

More than 5 years have passed since last update.

文字列中の   (C2A0) を正規表現で削除

Last updated at Posted at 2015-01-23

スクレイピングなどしているとテキスト中に含まれている   によく遭遇します。
これは strip でも gsub(/\s/'') 等でも消せませんが、以下のようにすると削除できます。

 # 正規表現でPOSIX文字クラスを使う
inner_text.gsub(/[[:space:]]/)

 # 文字コード直接指定でもできる
inner_text.gsub(/\u{C2A0}/)  # gsub(/\uC2A0/) でもOK

 
リファレンスよく読めばかいてありますね。
* リファレンスマニュアル - 正規表現

ここを読んでピンと来ました。
* Stack Overflow - How to remove whitespace from string in Ruby

24
23
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23