Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Rubyの外部ファイルから正規表現を読みたい。たとえば、ファイルからすべての正規表現をロードし、それぞれに対してgsubを実行する大きな文字列に置き換えたいと思います。各正規表現は、ファイルの改行で区切られます。
外部ファイルは次のようになります。
engenharia d[ae] computação ci[êe]ncias? d[ae] computação
出来ますか?
もちろん!を使用するだけで正規表現を作成できますRegex.new "my string"。ファイルを読むことと一緒にそれをまとめるには:
Regex.new "my string"
regexen = [] File.open("myfile.txt", "r") do |f| while line = f.gets.chomp regexen << Regexp.new line end end