1

How do you create a perl regex that matches the following conditions?

  1. Word length should be greater than 4 characters.
  2. Should not contain any non alphabetical characters (i.e. . - " , )

So words like "barbar..", "bar.", "ba.." should be rejected in matching.

Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
neversaint
  • 60,904
  • 137
  • 310
  • 477

3 Answers3

6

Do you mean for a word to be longer than 4 characters, and only to contain alpha-characters?

This will match 5 or more letters from a-z, non-case-sensitive:

/^[a-zA-Z]{5,}$/
Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
1

I would take Nightfirecat's answer and add word boundaries to it to catch words - his is for an entire string.

/\b[a-zA-Z]{5,}\b/
Bill Ruppert
  • 8,956
  • 7
  • 27
  • 44
  • This would work in the case that the match is supposed to be included as part of the string (and could be the whole string), where mine is specifically for if it's supposed to be the whole string. – Nightfirecat Nov 18 '11 at 03:18
0

Instead of alphabets, if you want to allow alphanumeric you can use this /^\w{5,}$/ (It will also match '_')

\w normally matches alphanumeric in ASCII. For some of the exceptions, see the answer by Sinan in this post How can I validate input to my Perl CGI script so I can safely pass it to the shell?

Community
  • 1
  • 1
SAN
  • 2,219
  • 4
  • 26
  • 32