tags:

views:

169

answers:

3

Im not sure how I can achieve this match expression. Currently I am using,

([A-Za-z0-9-]+)

...which matches letters and numbers. I would also like to match on dashes and underscores in the same expression. Anyone know how?

I would like to be able to match product_name and product-name

Thanks! George

+4  A: 

Your expression should already match dashes, because the final - will not be interpreted as a range operator (since the range has no end). To add underscores as well, try:

([A-Za-z0-9_-]+)
waxwing
+1  A: 

Just escape the dashes to prevent them from being interpreted (I don't think underscore needs escaping, but it can't hurt). You don't say which regex you are using.

([A-Za-z0-9\-\_]+)
John Knoeller
As a follow up, how can I apply the above rules while excluding periods? I would like to ignore, for instance, image.png
George
@George: This regex already doesn't match periods.
John Knoeller
+1  A: 

Depending on your regex variant, you might be able to do simply this:

([\w-]+)

Also, you probably don't need the parentheses unless this is part of a larger expression.

Mark Byers
This is for a .htaccess file, so I need the parentheses since as you said, it is in a larger expression :)
George