我正在编写一个脚本来搜索HTML文本块,%{}以便用动态变量替换它们.
出于某种原因,我的正则表达式无效.
str = "<p>%{urgent_personal_confidential}</p>"
regex = /^%\{(.*)\}/
puts str.match(regex)
Run Code Online (Sandbox Code Playgroud)
我确信这很简单......任何关于可能出错的想法?
Ruby已经将该功能内置到String中.
"The quick brown %{animal1} jumps over the lazy %{animal2}" % { :animal1 => "fox", :animal2 => "dog" }
Run Code Online (Sandbox Code Playgroud)
结果是
"The quick brown fox jumps over the lazy dog"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
807 次 |
| 最近记录: |