Reputation: 2840
Can someone help me write a regex that matches only all lower case letters plus hyphens.
Example: this-page-name
Upvotes: 13
Views: 20828
Reputation: 5696
Mike Clark's pattern [a-z\-]+
would match -start-dash-double-dash---and-end-dash-
Maybe ^[a-z]+(-[a-z]+)*$
is little bit more precise.
Upvotes: 33
Reputation: 11979
This will catch 1 or more characters that are either lowercase a-z or the hyphen
[a-z\-]+
The trick is to escape the hyphen with a backslash.
For completeness, you can add an appropriate boundary such as \b on each end to signify a full word match, or ^ and $ to make it match a full line.
Upvotes: 26