---
title: "Regular expression to match maximium of five words"  
description: "Regular expression to match maximium of five words"  
author: "Pravesh Singh"  
published: 2014-08-27  
updated: 2014-08-27  
canonical: https://www.mindstick.com/forum/2169/regular-expression-to-match-maximium-of-five-words  
category: "asp.net"  
tags: ["Regular expression to match maximium of five words", "regular expression", "regex", "regular expression matches the words"]  
reading_time: 1 minute  

---

# Regular expression to match maximium of five words

I have a [regular expression](https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java)

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

which validates that the [word](https://www.mindstick.com/forum/305/read-word-file) contains alpha-numeric [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) and few special characters and [length](https://www.mindstick.com/interview/1915/what-is-the-difference-between-char_length-and-length) should not be more than 5 characters.

How do I make this regular expression to [accept](https://yourviews.mindstick.com/view/81278/we-have-to-accept-that-roger-federer-is-the-richest-one) a maximum of five words matching the above regular expression.

## Replies

### Reply by Sumit Kesarwani

Hi Pravesh, \
try this:

^[a-zA-Z+#\-.0-9]{1,5}(\s[a-zA-Z+#\-.0-9]{1,5}){0,4}$


---

Original Source: https://www.mindstick.com/forum/2169/regular-expression-to-match-maximium-of-five-words

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
