What does these signs means


i know ([a-zA-Z]+) matches any characters lower or uppercase

But i would like to what do these below 2 signs means and what do they match ?

  1. ([a-zA-Z_]+)
  2. ([a-zA-Z_-]+)


First one also matches underscores (the _ )
Second one also matches underscores (the _ ) and the hypen (the - )

hi cp

is _ underscore sign optional in ([a-zA-Z_]+)


All of them are optional.
The + simply means one of the characters in found between needs to be found for the expression to be true, but it will capture all matching characters until the end or a non-matching character is found.\

To put that into perspective, the following will be captured


thanks cp

for the clarification


hi cp

one more question :

whats the difference in (.*) and (.+)


The first is for matching any character zero or more times, the latter is matching any character 1 or more times

If you want an AWESOME book for Regular Expressions, I recommend O’Reilly Regular Expression Pocket Reference

Also, regex basics are quite nicely explained on http://www.regular-expressions.info/
A lot of stuff I know about regex I got from there.


You really should add that Apache advises that the metacharacter (within a character range definition), the “-”, should be the first character within the range definition so it can be understood to be the character (rather than the metacharacter used to define a range of characters). Under common usage, however, Apache accepts the - as the last character in the range (as vineet has in his code snippet).

WARNING: The references to the regex book and article are fine but mod_rewrite only uses a rather small and very basic subset of regex BECAUSE the Apache variables are not multi-line, they are not formatted (italic, bold, etc) and they must fit the format of the variable as required by Apache. That makes learning the subset used by mod_rewrite a relatively trivial task.

As for the definitive book on regex, let me recommend what most consider the bible on this subject: Jeffrey Friedl’s “Mastering Regular Expressions” which is published by O’Reilly.



The reason I suggested the O’Reilly Regular Expression Pocket Reference is because it actually teaches you the regular expression syntax used by various languages (I think apache may be in there too, but I don’t quite remember and my book is on my work desk). So you can see the C# implementation, versus PHP, Perl, Java, etc. It is very handy when you are trying to nail down a regular expression and can’t quite remember some of the oddities a language may have implemented.

Even though this is in the URL Rewriting forum, I didn’t want to assume it was meant for Apache, versus maybe .NET URL Rewrite, or just a regular expression to be used for some other purpose. :slight_smile: