Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match an empty string?

I want to match and group any of these listed words:

aboutus/,race/,cruise/,westerlies/,weather/,reach/,gear/ or empty_string

Here is a solution, but which will not match the empty_string:

^(aboutus|race|cruise|westerlies|weather|reach|gear)/$

So my question is: How to include Empty string in this matching?

I still don't get a good solution for this.

So I added one more regex specially for empty_string:ie ^$.

Note: these regular expression is for django urls.py.

update: It will be better if the capturing group does not contain /

like image 381
suhailvs Avatar asked Dec 05 '22 07:12

suhailvs


1 Answers

try this:

^(aboutus|race|cruise|westerlies|weather|reach|gear)?/$

edit: if '/' is in every case except the empty string try this

^((aboutus|race|cruise|westerlies|weather|reach|gear)(/))?$
like image 134
Omair Shamshir Avatar answered Dec 08 '22 05:12

Omair Shamshir