1
0

More than 3 years have passed since last update.

正規表現 式展開

Last updated at Posted at 2019-11-24

正規表現内では、#{式} という形式で式を評価した文字列を埋め込むことが できます。

place = "東京都"
/#{place}/.match("Go to 東京都") # => #< MatchData "東京都">
埋め込んだ文字列にメタ文字が含まれているならば、それは メタ文字として認識されます。

number = "(\d+)"
operator = "(\+|-|\*|/)"
/#{number}#{operator}#{number}/.match("43+291") # => #< MatchData "43+291" 1:"43" 2:"+" 3:"291">
埋め込む文字列をリテラルとして認識させたい場合は Regexp.quote を 使います。

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