User name with a-z A-Z 0-9, and spaces?

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

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

I'm trying to put together a regex pattern to determine if a user name uses only alpha-numeric characters and spaces. I already have PHP programmed to determine if there is more then one space, if the user name starts or ends with the space. However if that can be determined with the regex that would be cool too.

Here are some of the things I've kind of been messing with...

Starts with alphabetical character...
^a-zA-Z

Spaces are optional since I have them worked out though I don't mind learning for anyone who wants to add to this...

I'd like to apply this to spaces...have an optional space but must not exceed more than a single instance. (I know a space is a literal, how do I define it then?)
{0,1}

I can determine the length just fine already so I'm not concerned about that.

Valid Matches (encased in double quotes)...
"JAB Creations"
"John Doe"
"Jane Doe123"
"mikewilliams"

Invalid Matches...
"nice day today" (two spaces)
"a .foryou"
"1start withanumber"

Suggestions please?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: User name with a-z A-Z 0-9, and spaces?

Post by VladSun »

So ... you don't want your users use usernames which start or end with spaces ...
But if you permit spaces within the username, whhy should you restrict successive spaces?

Code: Select all

^\w+[\w\s\d]*[\w\d]+$
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

Gah...you had to ask that didn't you? :P

In all honestly I can't recall why at this moment in time.

Offhand I think it had something to do with matching "JAB Creations" against "jabcreations" to prevent people duplicating or impersonating accounts. I'll probably remove the space and scan the database for user names without the space. In example my user name will be "JAB Creations" so if some...oh wait that's reverse! :| Talk about a wrench...maybe I'll have a second column where user names stripped of spaces are stored and check against that? Hmm...

I'm not sure what your regex suggestions actually matches? 8O
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: User name with a-z A-Z 0-9, and spaces?

Post by VladSun »

JAB Creations wrote:Gah...you had to ask that didn't you? :P
Yep ;)
JAB Creations wrote:I'm not sure what your regex suggestions actually matches? 8O
Usernames that:
1) start with a letter;
2) folllowed by a letter, or a number, or a white space character
3) edning with a letter or a number character

PS: http://www.weitz.de/regex-coach/ - you will be happy with that ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

I think my logic is flawed then?

Code: Select all

if (preg_match("/^\w+[\w\s\d]*[\w\d]+$", $username)) {echo "\n".$username." = regex match\n";} else {echo "\n".$username." = regex NOT match\n";}
I'm testing this via AJAX. :mrgreen: I click a button and a JavaScript alert pops up with the response text. I'm absolutely loving this stuff.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

No no no...I just forgot the ending slash. :|

Thanks, I'm testing this out now. :mrgreen:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

How does this regex look? :)

Code: Select all

^[a-zA-Z]+[a-zA-Z\s\d]*[a-zA-Z\d]+$
I've been doing a lot of testing and this one does not match periods, dashes, underscores, and also disallows alphabetical characters as the first!

Any suggestions for strings I could try testing it against? Regex is awesome once you get the hang of it! :mrgreen:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: User name with a-z A-Z 0-9, and spaces?

Post by VladSun »

Use the link I've given above :)
I'm sure you'll do it yourself ;)
It's high time you've started doing stuff like this by your own ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

NO DON'T LEAVE ME! :cry:

Haha JK! Wow this is faster then AJAX! I like how it highlights the matching *parts* of the string! I'm just blasting through testing now!

What does the tree do? Is this sort of like the top branch is the start/carrot match, the second the general matching, and the bottom branch the end matching?

The step part also helps dramatically! Way freaking cool! This program is so getting a place in my application installer archives! :mrgreen:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

I'm sort of stuck on * and $.

It seems * includes all the characters after the first including the last. How do I force * to not include the last character or do I want a different operator?

How do I make these two work together more constructively? Am I discussing the middle and ending branches of the tree so to speak? Going through the steps I've tried a few things though can't see to select the last character. :|
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: User name with a-z A-Z 0-9, and spaces?

Post by VladSun »

* - match previous match repated zero, one or more times;
$ - end of line;
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

Gah...I know that already...

So The Regex Coach has a tab called "tree".

I'm trying to discuss matching in three segments...the first, body, and last segment.

The issue is I'm not sure how to separate the "body" and "last" segments.

Do I put the $ before or after the brackets in example?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

This seems to have very minimal "repeating" in The Regex Coach's step tab.

Code: Select all

^[a-zA-Z][\w\s\d]*[\w\d]$
In example on the string "awe" it only executes 10 steps. :)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: User name with a-z A-Z 0-9, and spaces?

Post by JAB Creations »

However I can't use the character class abbreviations as it is undesirably matched an ampersand.

Code: Select all

^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]$
Any way I'll keep messing with it...wonder if I can get this down to three steps. :lol:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: User name with a-z A-Z 0-9, and spaces?

Post by prometheuzz »

JAB Creations wrote:...

Valid Matches (encased in double quotes)...
"JAB Creations"
"John Doe"
"Jane Doe123"
"mikewilliams"

Invalid Matches...
"nice day today" (two spaces)
"a .foryou"
"1start withanumber"

Suggestions please?
This regex will do just that:

Code: Select all

^(?!(\S* ){2})[a-zA-Z][a-zA-Z\d\s]*$
Post Reply