如何让 gsub 处理多个模式和替换 [英] How to have gsub handle multiple patterns and replacements

查看:24
本文介绍了如何让 gsub 处理多个模式和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前,我在 PHP 中创建了一个函数,用于推特化"通过 Twitter 的 API 提取的推文文本.

A while ago I created a function in PHP to "twitterize" the text of tweets pulled via Twitter's API.

这是它的样子:

function twitterize($tweet){
$patterns = array ( "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/", 
                    "/(?<=^|(?<=[^a-zA-Z0-9-\.]))@([A-Za-z_]+[A-Za-z0-9_]+)/",
                    "/(?<=^|(?<=[^a-zA-Z0-9-\.]))#([A-Za-z_]+[A-Za-z0-9_]+)/");
$replacements = array ("<a href='\\0' target='_blank'>\\0</a>", "<a href='http://twitter.com/\\1' target='_blank'>\\0</a>", "<a href='http://twitter.com/search?q=\\1&src=hash' target='_blank'>\\0</a>");

return preg_replace($patterns, $replacements, $tweet);

}

现在我对 Ruby 的 gsub 有点卡住了,我尝试了:

Now I'm a little stuck with Ruby's gsub, I tried:

def twitterize(text)
patterns = ["/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/", "/(?<=^|(?<=[^a-zA-Z0-9-\.]))@([A-Za-z_]+[A-Za-z0-9_]+)/", "/(?<=^|(?<=[^a-zA-Z0-9-\.]))#([A-Za-z_]+[A-Za-z0-9_]+)/"]
replacements =  ["<a href='\\0' target='_blank'>\\0</a>",
                "<a href='http://twitter.com/\\1' target='_blank'>\\0</a>",
                "<a href='http://twitter.com/search?q=\\1&src=hash' target='_blank'>\\0</a>"]

return text.gsub(patterns, replacements)
end

这显然不起作用并返回错误:

Which obviously didn't work and returned an error:

No implicit conversion of Array into String

在查看 Ruby 文档后对于 gsub 并探索了他们提供的一些示例,我仍然找不到解决我的问题的方法:我如何让 gsub 处理多个一次模式和多个替换?

And after looking at the Ruby documentation for gsub and exploring a few of the examples they were providing, I still couldn't find a solution to my problem: How can I have gsub handle multiple patterns and multiple replacements at once?

推荐答案

好吧,正如您从文档中看到的那样,gsub 处理多个模式和替换在一次.这就是导致您错误的原因,否则很明显(您可以将其读作给我一个字符串,而不是一个数组!!1").

Well, as you can read from the docs, gsub does not handle multiple patterns and replacements at once. That's what causing your error, quite explicit otherwise (you can read that as "give me a String, not an Array!!1").

你可以这样写:

def twitterize(text)
  patterns = [/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, /(?<=^|(?<=[^a-zA-Z0-9-\.]))@([A-Za-z_]+[A-Za-z0-9_]+)/, /(?<=^|(?<=[^a-zA-Z0-9-\.]))#([A-Za-z_]+[A-Za-z0-9_]+)/]
  replacements =  ["<a href='\\0' target='_blank'>\\0</a>",
            "<a href='http://twitter.com/\\1' target='_blank'>\\0</a>",
            "<a href='http://twitter.com/search?q=\\1&src=hash' target='_blank'>\\0</a>"]

  patterns.each_with_index do |pattern, i|
    text.gsub!(pattern, replacements[i])
  end

  text
end

这可以重构为更优雅的 ruby​​ish 代码,但我认为它可以完成这项工作.

This can be refactored into more elegant rubyish code, but I think it'll do the job.

这篇关于如何让 gsub 处理多个模式和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆