www.example.com" />

将 www.example.com 替换为 www.example.com

发布于 2024-08-08 03:32:52 字数 313 浏览 4 评论 0 原文

我不太擅长正则表达式,但我需要将以下示例从这里转换

<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>

I am not very good at regex, but I need to convert the following example from this

<li>Creations by Carol - www.driedfloralcreations.com</li>

to

<li>Creations by Carol - <a href="http://www.driedfloralcreations.com" rel="external">www.driedfloralcreations.com</a></li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流殇 2024-08-15 03:32:52

如果您只是在

  • 元素中查找格式类似于问题中的 URL,那么它应该比许多其他建议的解决方案简单得多。我认为您实际上并不需要验证您的 URL,您只想获取网站名称和 URL 的列表并将 URL 转换为链接。
  • 您的搜索模式可以是:

    <li>(.+) - (https?:\/\/)?(\S+?)<\/li>
    

    替换模式可以是:

    <li>$1 - <a href="(?2:$2:http\://)$3" rel="external">$3</a></li>
    

    刚刚在 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:

    <li>(.+) - (https?:\/\/)?(\S+?)<\/li>
    

    And the replace pattern would be:

    <li>$1 - <a href="(?2:$2:http\://)$3" rel="external">$3</a></li>
    

    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.

    自演自醉 2024-08-15 03:32:52

    PHP 中这个怎么样?

    $string = '<li>Creations by Carol - www.driedfloralcreations.com</li>';
    $pattern = '/(www\.[a-z\d-\.]+\.[a-z]+)/i';
    $replacement = '<a href="http://$1" rel="external">$1</a>';
    echo preg_replace($pattern, $replacement, $string);
    

    假设您的链接始终是 www.something.extension。

    How about this in PHP?

    $string = '<li>Creations by Carol - www.driedfloralcreations.com</li>';
    $pattern = '/(www\.[a-z\d-\.]+\.[a-z]+)/i';
    $replacement = '<a href="http://$1" rel="external">$1</a>';
    echo preg_replace($pattern, $replacement, $string);
    

    Assumes your links are always www.something.extension.

    月依秋水 2024-08-15 03:32:52

    您必须非常清楚需要为正则表达式提供多少信息以避免误报。

    例如,模式 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.

    心病无药医 2024-08-15 03:32:52
    www\.[a-zA-Z0-9_-]+\.(fr|com|org|be|biz|info|getthelistsomewhere)
    
    www\.[a-zA-Z0-9_-]+\.(fr|com|org|be|biz|info|getthelistsomewhere)
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文