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