将 www.example.com 替换为 www.example.com
我不太擅长正则表达式,但我需要将以下示例从这里转换
<li>Creations by Carol - www.driedfloralcreations.com</li>
为
<li>Creations by Carol - <a href="http://www.driedfloralcreations.com" rel="external">www.driedfloralcreations.com</a></li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只是在
元素中查找格式类似于问题中的 URL,那么它应该比许多其他建议的解决方案简单得多。我认为您实际上并不需要验证您的 URL,您只想获取网站名称和 URL 的列表并将 URL 转换为链接。
您的搜索模式可以是:
替换模式可以是:
刚刚在 TextMate 中测试了查找/替换,效果很好。如果
http://
尚不存在,它会添加它,否则假设-
之后的内容都是 URL,只要它不包含空格即可。对于测试正则表达式,Rubular 是一个很棒的工具。您可以粘贴一些文本,当您键入正则表达式时,它会显示匹配的内容。它是一个 ruby 工具,但 TextMate 使用与 ruby 相同的正则表达式语法。
If you're only looking for URLs in
<li>
elements formatted like the one in your question, it should be much simpler than a lot of the other suggested solutions. You don't really need to validate your URLs, I assume, you just want to take a list of site names and URLs and turn the URLs into links.Your search pattern could be:
And the replace pattern would be:
Just tested the find/replace out in TextMate and it worked nicely. It addes
http://
if it isn't already present, and otherwise assumes that whatever is after the-
is a URL as long as it doesn't contain a space.For testing out regular expressions, Rubular is a great tool. You can paste in some text, and it'll show you what matches as you type your regex. It's a ruby tool, but TextMate uses the same regex syntax as ruby.
PHP 中这个怎么样?
假设您的链接始终是 www.something.extension。
How about this in PHP?
Assumes your links are always www.something.extension.
您必须非常清楚需要为正则表达式提供多少信息以避免误报。
例如,模式 www.something.somethingelse 就足够了吗?文件中还有其他 www 会被捕获吗?
也许
是正确的匹配。如果不知道您的整个文件,我们就无法猜测。可能还有其他
You have to be really clear about how much information you need to give the regex to avoid false positives.
For example is the pattern www.something.somethingelse enough? are there other www in the file that would get caught?
maybe <li> something - somethingelse</li> is the correct match. We cannot guess without knowing your whole file. There might be other <li> in there that you don't want to change.