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
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

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

Post by Zoxive »

http://en.wikipedia.org/wiki/Regular_expression

You can also google regex etc.. to find more and learn what does what.
I can pretty much tell you everyone here helping you right now, has learned through trying it & searching internet.

Some online regex tools i like: http://regex.larsolavtorvik.com/ , http://regexpal.com/
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Post by GeertDD »

I would probably break down validation in multiple steps. Even though you probably can create a regex that checks for the amount of spaces, letters and string length, I think using native php functions like substr_count() and mb_strlen() in combination with one simple regex would keep your code readable and your regex fast.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

Are you seriously intending to do a regular expression check against every username in your database to make sure there aren't any similar ones? I do hope the system never gets popular, it'll be hellishly slow.
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 »

onion2k wrote:Are you seriously intending to do a regular expression check against every username in your database to make sure there aren't any similar ones? I do hope the system never gets popular, it'll be hellishly slow.
onion2k, I think this answer is for another thread. ;)
viewtopic.php?f=19&t=88158
There are 10 types of people in this world, those who understand binary and those who don't
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 »

Code: Select all

^[A-z](\w|(\s)(?!.*\2))+\w$
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 »

^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]$
19 Steps on "JABCreations"

^(?!(\S* ){2})[a-zA-Z][a-zA-Z\d\s]*$
48 Steps on "JABCreations"

Like VladSun kindly suggested download The Regex Coach. :)
http://www.weitz.de/regex-coach/
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:^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]$
19 Steps on "JABCreations"

...
That regex does not do what you posted in your original post.
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 »

prometheuzz wrote:That regex does not do what you posted in your original post.
True however I'm thinking of creating a second MySQL column where letter casing is converted to lower case and spaces are stripped. I've been sort of feeling my way in my head with the discussions we've had here. One of the other threads we have been discussing about how to prevent for practical purposes the same name. For example I consider "jabcreations" and "JAB Creations" the same thing. I know Linux-heads don't like this as even a single different letter casing is considered different. However Linux doesn't get people and people don't get Linux in this regards and I have the steps worked out to minimize server load as much as possible at least on my current skill level.

Thank you for your input of course! I have messed with The Regex Coach's steps tab with all the regex strings posted here trying to minimize the steps needed. Frankly you should only be peeved if I simply copied and pasted your regex suggestion because then I would clearly have no interest in learning to do it on my own and to of course get better at it. I think my slightly shifting goals are well within reason. For me I learn best with multiple working examples. They don't have to be exactly what I need or want but as long as they work I can learn from them. Thanks again! :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

JAB Creations wrote:For example I consider "jabcreations" and "JAB Creations" the same thing.
Can I just ask ... why didn't you go with the SOUNDEX() solution I posted? It really would be perfect for what you're talking about. And fast, especially if you stored the SOUNDEX() value when you created the user record.
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 »

In regards to SOUNDEX() would "Robert" and "Rupert" be considered the same name?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

JAB Creations wrote:In regards to SOUNDEX() would "Robert" and "Rupert" be considered the same name?
Yes. But they are similar, so that's perfectly correct.
Post Reply