The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. November 21, 2008, 05:11:46 PM

Login with username, password and session length


Pages: 1 [2]
  Print  
Author Topic: Little help with a regex...  (Read 1970 times)
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3384


View Profile
« Reply #15 on: January 16, 2008, 08:20:25 AM »

no there is no difference between [0-9] and \d i just use [0-9] because it makes more sense to read it and is less prone to confusion.

so yes  preg_match('/^[DMY]\d+$/'); would be correct, but there is no difference. I don't even think execution is any different, because i think \d gets translated into [0-9]. So it might even slow it down 1 step to do \d but i dont know really?

the caps thing is why I put the 'i' at the end of the pattern, so it would match upper or lower. but if it is required to be upper, no I is needed.

\w is a word character, so the assumption of Dbyter is that your pattern will be followed by word characters. However that would not match "D12323 somethign" because of the space.

i don't know why i used (d|m|y) instead of [dmy]. it should be the latter.
Also, my use of ? was dumb. since its only digits that can be matched, and you want all of them when they occur in succession, then you don't need the ? to limit it. The minute it hits a non-digit, it starts trying to match the rest of the pattern, which should be anything but a digit or the nothing at all, in which case the last digit matched should be the end of the pattern. so $

so perks is best if you need only uppers and will require at least one digit to follow:  preg_match('/^[DMY][0-9]+$/');
add an i after it all if you need either upper or lower: preg_match('/^[DMY][0-9]+$/i');

also not sure if you know, you can use anything as your delimiter. I hate using / because it gets REALLY confusing with /http:\/\/www\.whathehell\.com/
so i use # usually #http://www\.whathehell\.com#
so the final would be preg_match('#^[DMY][0-9]+$#');
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5142


:sniffle: Humor was so much easier before.


View Profile
« Reply #16 on: January 16, 2008, 02:51:55 PM »

lol, I can see you are getting pissed with this perk  ROFLMAO

@ TD: Not at all man, just had to move quick and wanted to weigh in. Lots of stuff happening around me and don't want to be gone, but not lots of time ATM.

@ Nuts: agree, the 0-9 is (at least for me) much more readable. I like sets rather than magics if I can avoid it. Also excellent point to remember - I often use the tilde as my delimiter as well because I am rarely looking for it.
Logged

If I can't be Mr. Root then I don't want to play.
thedarkness
Global Moderator
Lifer
*****
Offline Offline

Posts: 580



View Profile
« Reply #17 on: January 16, 2008, 08:40:03 PM »

I use '%' as my delimeter most of the time. Well I guess that just about wraps up this thread, interesting IMHO. I think there's a FUD factor with regex that takes a while to get over. it seems so complex at first that people tend to get very confused, I know I did (some would say i still do), thing is there are a lot of regexs out there that work, do what they are supposed to do, but are technically incorrect or "wanting", just depends how anal you want to be I think ;-)

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: 1 [2]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!