Regular Expression Pattern Problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
CyberSpatium
Forum Newbie
Posts: 21
Joined: Thu Mar 20, 2003 12:23 pm
Contact:

Regular Expression Pattern Problem

Post by CyberSpatium »

I am having a heck of a time finding a regular expression pattern that works for a simple text string. i have a form field where users enter their name. if can be just their first name, first and last name, or just a nickname (max length is 30 characters).

I am trying to use ereg_match() to verify it is in the correct format. but I have tried pattern after pattern and I cannot get one to work.

any help would be greatly appreciated.


CyberSpatium
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

And what exactly would the pattern be? (Not in RE but in first five characters are only a,b,c and so on...) Simply restricting the length to be between 1 and 30 is as simple as: ^.{1,30}$ (strlen seems more appropriate for this task)
CyberSpatium
Forum Newbie
Posts: 21
Joined: Thu Mar 20, 2003 12:23 pm
Contact:

Post by CyberSpatium »

well, for security reasons, i am checking it to maker sure that only text has been submittted by the form, nothing else. so, the only thing allowed to be submitted text and a space, and that is what i am trying to limit it to with this pattern, but I have not been able to get one to work.

thx
cyberspatium
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

[a-zA-Z ]{1,30}
This should work, but it's not perfect - you'll likely want to give it some more thought and some fine tuning.

play here: http://www.cuneytyilmaz.com/prog/jrx/
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Regular Expression Pattern Problem

Post by stereofrog »

CyberSpatium wrote:I am having a heck of a time finding a regular expression pattern that works for a simple text string. i have a form field where users enter their name. if can be just their first name, first and last name, or just a nickname (max length is 30 characters).

I am trying to use ereg_match() to verify it is in the correct format. but I have tried pattern after pattern and I cannot get one to work.

any help would be greatly appreciated.


CyberSpatium
You have to define "name" more exactly. Are names like "O'Malley-Finkel-Harris-Smith" allowed? Is "^%&^%@#" a valid nickname?

[a-zA-Z ]{1,30}
This will match, among others, the string of 30 spaces.
CyberSpatium
Forum Newbie
Posts: 21
Joined: Thu Mar 20, 2003 12:23 pm
Contact:

Post by CyberSpatium »

the posts will be in this format:

firstname lastname
firstname
lastname
Firstname Lastname
Firstname
Lastname
nickname
Nickname

thx,
cyberspatium
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

So you don't want Tim O'Reilly as a customer?
CyberSpatium
Forum Newbie
Posts: 21
Joined: Thu Mar 20, 2003 12:23 pm
Contact:

Post by CyberSpatium »

true, did not think of that
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

And what about non-English alphabets?

Kimi Räikkönen
Mika Häkkinen
Post Reply