9
6

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 文字列から指定の文字以降を削除する

Last updated at Posted at 2017-08-11

例)strという文字列のABCから後ろを削除したい時

str = "123456789_ABC_abcdefghijklmnop"

p str.sub!(/(.*)ABC.*/){$1}

str.sub!(/ABC.*/m, "")

オプション m は . が改行にもマッチするようにという意味らしいです。

結果↓

=> "123456789_"
9
6
2

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
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?