You regex has a small bug. It matches letters, digits, +
, #
, period
but not hyphen
and also all char between #
and period
. This is because hyphen
in a char class when surrounded on both sides acts as a range meta char. To avoid this you'll have to escape the hyphen
:
^[a-zA-Z+#\-.0-9]{1,5}$
Or put it at the beg/end of the char class, so that its treated literally:
^[-a-zA-Z+#-.0-9]{1,5}$
^[a-zA-Z+#.0-9-]{1,5}$
Now to match a max of 5 such words you can use:
^(?:[a-zA-Z+#\-.0-9]{1,5}\s+){1,5}$
EDIT: This solution has a severe limitation of matching only those input that end in white space!!! To overcome this limitation you can see the ans by Jakob.