I've been really struggling with regex lately ... I never quite got the hang of it. I'm currently hung up on trying to match on the following in my .htaccess file:

^foo/([A-Za-z0-9]+)/([0-9]+)$

The catch is that I want the alphanumeric string to NOT be one of a short list of words i.e. (bar|baz|bat)

How can I match on any alphanumeric word as long as it is not in a list?

Dani AI

Generated

As discovered, a negative lookahead is the right tool for “match anything except these exact tokens.” The lookahead in post #2 works in principle but the repeated-dot construct can accidentally allow non-alphanumeric characters and may introduce unnecessary backtracking. A clearer, safer approach is to assert the forbidden words immediately after the fixed prefix, then match the allowed token with a simple character class.

A compact, readable pattern (used as the RewriteRule pattern) is:

^foo/(?!bar/|baz/|bat/)([A-Za-z0-9]+)/([0-9]+)$

Explanation: the (?!bar/|baz/|bat/) negative lookahead makes sure the next path segment is not exactly one of those words (the trailing slash is used as the boundary). The first capture then matches one or more alphanumerics and the second capture matches the digits.

An alternative that is often easier to read and maintain is to exclude the bad tokens with a RewriteCond and keep the RewriteRule simple. For example, check REQUEST_URI (which includes a leading slash) and then run a general rule that captures the token and id. Using a condition avoids complex inline lookarounds and makes the exception list obvious.

Notes and tips:

  • In .htaccess the RewriteRule pattern is matched without a leading slash; REQUEST_URI contains the leading slash — account for that.
  • Use [NC] for case-insensitive matching if needed, or expand the excluded words accordingly.
  • If underscores or hyphens should be allowed, replace the token class (e.g. use \w or [A-Za-z0-9_-]).
  • Test with cases like foo/bar/123 (should not match) and foo/barista/123 (should match).
  • Keep exception lists short in-pattern; for many exclusions prefer a RewriteCond or a small lookup table to avoid long, hard-to-read regexes.

Echoing and : the approach is solid — just tighten the pattern for correctness and performance.

Recommended Answers

All 3 Replies

I did some more investigating and was able to get it to work (I think) with lookahead??

^foo/((?!\b(bar|baz|bat)\b).)*/([0-9]+)$

It seems to work but can someone confirm that I did it correctly??

On my phone ATM so can't test it out or anything. But in principle, it looks OK to me. Might be worth getting another opinion though. I'm a little rusty with regex's myself! :)

Well, in all honesty I thought you had been banging your head on the keyboard when I first saw it. :D

After seeing this and thinking about it, this is fascinating methodology, and I've begun studying it.

This subject is still way above me so I can't make a conclusive comment here, but I did find this regular expression tester. At the moment I can't see how it works, and it may not be advanced enough for what you are looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.