1
1

More than 3 years have passed since last update.

応用カリキュラム 04 ruby正規表現

Last updated at Posted at 2019-11-19

sub(≒replace)

文字列の指定部分を別の文字列に置換。

gsub

文字列の指定部分を全て別の文字列に置換。

str = "aaabbb"
str.sub(/bbb/,"BBB")
# aaaBBB

str.gsub(/b/,"B")
# aaaBBB

match

検索したい文字が含まれているかどうか。
見つかれば、検索文字リピート
見つからないなら、nil

str = "aaabbb"
str.match("ab")
# ab

検索条件

  • [a-z] a,b,c,d・・・x,y,zのいずれかの文字にマッチ。
  • \d 数字にマッチ
  • {n, m} 直前の文字が最低n回、最高m回出現
  • i 大文字・小文字を区別しない
  • . あらゆる1文字
  • 直前文字の1回以上の繰り返し

複合条件可。

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