使用 RegExp 反转匹配

发布于 2024-09-15 20:20:56 字数 290 浏览 4 评论 0原文

我目前正在 Yahoo! 上开发管道。管道。我有一个正则表达式来匹配一个 URL。现在,我需要匹配不匹配的才能将其删除,这样就只有我的网址了。我的正则表达式是:

[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+

如何反转匹配? (它必须通过另一个 RegExp 来完成,YPipes 不支持任何其他方式。)

编辑:为了澄清:我得到了一个字符串,需要获取其中的第一个 URL。这就是为什么其他方法都不起作用......

I'm currently working on a pipe on Yahoo! Pipes. I got a RegExp to match an URL. Now, I need to match the unmatched in order to delete it, so that there is only my URL. My RegExp is:

[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+

How do I invert the matching? (It has to be done via another RegExp, YPipes doesn't support any other way.)

edit: For clarification: I got a string and need to get the first URL inside of it. That's why nothing else works…

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

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

发布评论

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

评论(1

波浪屿的海角声 2024-09-22 20:20:56

Yahoo Pipes 支持正则表达式替换(使用字符串正则表达式模块)。

然后匹配您想要保留的部分并将其捕获到一个组中。您已经在这样做了,尽管您的正则表达式本身略有错误,但这里至少纠正了明显的错误:

([A-Za-z]+://[A-Za-z0-9_-]+\.[A-Za-z0-9_:%&?/.=-]+)
^          ^            ^
|          |            |
|          |            + lone dashes always at the end of the character class!
|          +--- forward slashes do not need to be escaped             
+--- capturing group 1, contents later accessible through $1

然后用该组的内容替换整个字符串。只需将 $1 放入“替换为”字段即可。这实际上会删除您不想保留的所有内容。

由于我不确定您的正则表达式实际上应该做什么,因此我无法为您提供更好的版本。

一般提示:正则表达式始终且仅涉及匹配内容,而绝不会涉及不匹配(即“排除”)内容。有些正则表达式具有排除效果,但即使它们也是通过匹配来实现的。

Yahoo Pipes supports regex replace (use a String Regex module).

Then match the part you want to keep and capture it in a group. You are already doing this, although your regex itself is slightly wrong, here is one that at least has the obvious errors corrected:

([A-Za-z]+://[A-Za-z0-9_-]+\.[A-Za-z0-9_:%&?/.=-]+)
^          ^            ^
|          |            |
|          |            + lone dashes always at the end of the character class!
|          +--- forward slashes do not need to be escaped             
+--- capturing group 1, contents later accessible through $1

and then replace the entire string with the contents of that group. Just put $1 into the "replace with" field. Effectively this removes everything you did not want to keep.

Since I am not sure what your regex should actually do, I can't give you a better version.

As a general hint: Regex is always and only about matching stuff, and never ever about not matching (i.e. "excluding") stuff. There are regexes that have an excluding effect, but even they achieve it through matching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文