使用 RegExp 反转匹配
我目前正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Yahoo Pipes 支持正则表达式替换(使用字符串正则表达式模块)。
然后匹配您想要保留的部分并将其捕获到一个组中。您已经在这样做了,尽管您的正则表达式本身略有错误,但这里至少纠正了明显的错误:
然后用该组的内容替换整个字符串。只需将
$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:
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.