neversaint
neversaint

Reputation: 64014

Perl Regex that limits words Length

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.

Upvotes: 1

Views: 1730

Answers (3)

SAN
SAN

Reputation: 2247

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?

Upvotes: 0

Bill Ruppert
Bill Ruppert

Reputation: 9016

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/

Upvotes: 1

Nightfirecat
Nightfirecat

Reputation: 11610

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,}$/

Upvotes: 6

Related Questions