0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby] よくある区切り文字で文字列を分割したい

Posted at

自分的よくある区切り文字: カンマ、改行、半角スペース、全角スペース

> str = "hoge fuga,piyo\nfoo  bar, baz hogehoge"
> str.split(/[,\n\p{blank}]+/)
 => ["hoge", "fuga", "piyo", "foo", "bar", "baz", "hogehoge"]

\p{blank}

Unicodeの文字プロパティの指定方法で、半角/全角スペースに対応しています。

参考(blankについては書いてません :bow: ):
Unicodeの文字プロパティを指定した正規表現をみてみる(ECMAScript2018)

スペースに改行も含む \p{White_Space} がありますが、別に扱うことも多いのでblankを覚えたらよさそう。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?