Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match maximium of five words

Tags:

regex

asp.net

I have a regular expression

^[a-zA-Z+#-.0-9]{1,5}$

which validates that the word contains alpha-numeric characters and few special characters and length should not be more than 5 characters.

How do I make this regular expression to accept a maximum of five words matching the above regular expression.

like image 797
KhanS Avatar asked Feb 03 '26 06:02

KhanS


1 Answers

^[a-zA-Z+#\-.0-9]{1,5}(\s[a-zA-Z+#\-.0-9]{1,5}){0,4}$

Also, you could use for example [ ] instead of \s if you just want to accept space, not tab and newline. And you could write [ ]+ (or \s+) for any number of spaces (or whitespaces), not just one.

Edit: Removed the invalid solution and fixed the bug mentioned by unicornaddict.

like image 123
Jakob Avatar answered Feb 04 '26 21:02

Jakob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!