LoginSignup
9
7

More than 5 years have passed since last update.

Ruby で PHP の preg_match_all 関数と同じようなことをする

Posted at

例えば、下記のような文字列があったとして、li タグの中の文字列を抜き出したい場合、scan メソッドを使うことで PHP の preg_match_all 関数と同じようなことができます。

string = '<ul><li>埼玉県</li><li>東京都</li><li>千葉県</li></ul>'
string.scan(/<li>(.*?)<\/li>/).each do |item|
  p item
end

実行結果です。

["埼玉県"]
["東京都"]
["千葉県"]

下記を参考にしました。ありがとうございます。
http://stackoverflow.com/questions/5791718/preg-match-all-and-preg-replace-in-ruby

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