tags:

views:

40

answers:

4

Rules for the regex in english:

  • min length = 3
  • max length = 6
  • only letters from ASCII table, non-numeric

My initial attempt:

[A-Za-z]{3-6}

A second attempt

\w{3-6}

This regex will be used to validate input strings from a HTML form (i.e. validating an input field).

A: 

first one should work,second one will include digits as well,but you want to check non-numeric strings.

Anil Vishnoi
+2  A: 

A modification to your first one would be more appropriate

\b[A-Za-z]{3,6}\b

The \b mark the word boundaries and avoid matching for example 'abcdef' from 'abcdefgh'. Also note the comma between '3' and '6' instead of '-'.

The problem with your second attempt is that it would include numeric characters as well, has no word boundaries again and the hypen between '3' and '6' is incorrect.

Edit: The regex I suggested is helpful if you are trying to match the words from some text. For validation etc if you want to decide if a string matches your criteria you will have to use

^[A-Za-z]{3,6}$
Gopi
For validation, it is more likely the OP needs `^...$`, but there aren't enough details in the question. `\b` is useful to capture a short word from a string, which seems less common.
Kobi
@kobi agreed. would add it to my answer
Gopi
Or even `/^[a-z]{3,6}$/i`
Álvaro G. Vicario
A: 

First one should be modified as below

([A-Za-z]{3,6})

Second one will allow numbers, which I think you don't want to?

Sandy
+1  A: 

I don't know which regex engine you are using (this would be useful information in your question), but your initial attempt will match all alphabetic strings longer than three characters. You'll want to include word-boundary markers such as \<[A-Za-z]{3,6}\>.

The markers vary from engine to engine, so consult the documentation for your particular engine (or update your question).

Johnsyweb