Antimatch with Regex

大憨熊 提交于 2019-12-21 21:55:04

问题


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]+)$

But the .* should not match TaggedObjects.

Thanks for any advices.


回答1:


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

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

(?:(?!TaggedObjects).)*

In your example,

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

will match

index.php?page=blahblah&tagID=1234

and will not match

index.php?page=blahTaggedObjectsblah&tagID=1234

If you do want to allow that match and only exclude the exact string TaggedObjects, then use

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



回答2:


Try this. I think you mean you want to fail the match if the string contains an occurence of 'TaggedObjects'

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


来源:https://stackoverflow.com/questions/4660818/antimatch-with-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!