与正则表达式反匹配 [英] Antimatch with Regex

查看:74
本文介绍了与正则表达式反匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一个正则表达式模式,它不应该匹配一个组,而是匹配其他所有内容.
以下正则表达式模式基本有效:

I search for a regex pattern, which shouldn't match a group but everything else.
Following regex pattern works basicly:

index\.php\?page=(?:.*)&tagID=([0-9]+)$

但是 .* 不应该匹配 TaggedObjects.

But the .* should not match TaggedObjects.

感谢您的建议.

推荐答案

(?:.*) 是不必要的 - 你没有分组任何东西,所以 .*意思完全一样.但这不是您问题的答案.

(?:.*) is unnecessary - you're not grouping anything, so .* means exactly the same. But that's not the answer to your question.

要匹配不包含另一个预定义字符串(比如TaggedObjects)的任何字符串,请使用

To match any string that does not contain another predefined string (say TaggedObjects), use

(?:(?!TaggedObjects).)*

在你的例子中,

index\.php\?page=(?:(?!TaggedObjects).)*&tagID=([0-9]+)$

将匹配

index.php?page=blahblah&tagID=1234

并且不会匹配

index.php?page=blahTaggedObjectsblah&tagID=1234

<小时>

如果您确实希望允许该匹配并且只排除确切的字符串 TaggedObjects,则使用

index\.php\?page=(?!TaggedObjects&tagID=([0-9]+)$).*&tagID=([0-9]+)$

这篇关于与正则表达式反匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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