Stumped by Regex

They have: 36 posts

Joined: Oct 2004

Hello, Hope someone can help.
I need to find a regex to take a surname (any surname) from a form with ,
It must take "double-barrel" names into account (ie those with a space in the middle), but the whole string must be no longer than 20 characters. It must start with a letter and end with a letter with the space (or no space) appearing somewhere in the middle. It must also cater for normal single word surnames.

This is what i have so far (but doesnt limit to 20 characters)

m/^([a-zA-Z]\s?[a-zA-Z]?){1,20}$/

I bet this is simple but for the life of me i cant figure out the regex i need.

TIA

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

The most straightforward way to apply the upper limit is to check [incode]if length $_ <= 20[/incode]. If it passes that test, run your regex without any max arguments. Else, handle the case however you like. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.