2

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.

CSchulz
  • 10,882
  • 11
  • 60
  • 114

2 Answers2

5

(?:.*) 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]+)$
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    it could be just `(?!TaggedObjects).*` according to your first point. – SilentGhost Jan 11 '11 at 18:02
  • @SilentGhost: No, the repetition is outside of the group (in order to apply the negative lookahead at every position that follows). An alternative to `(?:(?!TaggedObjects).)*` would be `(?!.*TaggedObjects).*`. though. – Tim Pietzcker Jan 11 '11 at 18:11
  • I dont see a point in putting a negative lookahead (?!) inside a passive group (?:) Because the negative lookahead wont be captured anyway. Why do you put a single "." inside the passive group? why not .*? – Jaskirat Jan 11 '11 at 18:14
  • @Jass: See my previous comment - it ensures that the lookahead is applied at every position in the match, not just the beginning. But `(?!.*TaggedExpression)` works, too (as I wrote above). – Tim Pietzcker Jan 11 '11 at 18:50
1

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]+)$
Jaskirat
  • 1,114
  • 1
  • 8
  • 17