I have a problem with regular expressions. I would like to match strings that will represent pages in URL.
I want to match strings like these:
- article
- article-some
- article-some-more
- article-some-more-text
- a
- a-r-t-i-c-l-e
And avoid strings like these:
- -article
- article-
- article–some
- article-some–more
So basically all I need is a string that starts with [a-z], ends with [a-z], and can have minus sign in the middle. But I need multiple minus signs.
I tried this:
^([a-z0-9]+)(\-[a-z0-9]+)*([a-z0-9]+)?$
This works now, I opened a tab with Rubular, to paste what I was trying and came up with idea and solve this problem
But anyway, is there any other, more elegant way of doing this?
You can replace
0-9in your character classes with\d– it stands for ‘digit’ but means the same thing. You can also remove the last([a-z0-9]+)?, it is completely unnecessary because the block immediately before it ends with the same character class. If you’re OK with capital letters too (which you may not be, you didn’t specify) you can replace the character classes with[\w\d]which means any letter (‘word character’) or digit.