我正在从 php 过渡到 ruby,我试图找出 ruby 中 php 命令 preg_match_all 和 preg_replace 的同源。
太感谢了!
Ruby 中的等价物preg_match_all
is String#scan http://ruby-doc.org/core/classes/String.html#M001181,像这样:
In PHP:
$result = preg_match_all('/some(regex)here/i',
$str, $matches);
在红宝石中:
result = str.scan(/some(regex)here/i)
result
现在包含一个匹配数组。
Ruby 中的等价物preg_replace
is String#gsub http://ruby-doc.org/core/classes/String.html#M001186,像这样:
In PHP:
$result = preg_replace("some(regex)here/", "replace_str", $str);
在红宝石中:
result = str.gsub(/some(regex)here/, 'replace_str')
result
现在包含带有替换文本的新字符串。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)