The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 01:53:02 AM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Little help with a regex...  (Read 8701 times)
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« on: December 16, 2007, 02:04:47 PM »

Hi all, sorry for the rubbish post title but i've no clue what else to call this!

Pretty new to regular expressions (and do everything in my power to avoid them normally), and can't seem to figureout how to get it to do what I want, any help would be appreciated.

I'm looking to validate a string by checking it matches the following pattern and rules;

- String must start with either D, M or Y (singular)
- Above must be followed by at least one number
- First condition must only be followed by numbers

I've got this so far, but because im matching a 'pattern' it will validate things like M333D333 - I havent managed to get the bit to say "followed by any number of digits, and only digits to the end of the string.

Code:
preg_match('/^(d|m|y)[0-9]/'

Should probably say I'm using whatever the default PHP regular expression engine is. Any tips would be great - this obscure language is driving me insane!

Cheers,

DM
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: December 16, 2007, 06:39:41 PM »

preg_match('/^(d|m|y)[0-9]*?$/'
or
preg_match('/^(d|m|y)[0-9]*?\w/'

*? match the previous as many times as needed without overlapping the next thingy. non-greedy repeater match.
$ is the end of string
\w is word boundry, which could be any whitespace type of character plus symbols.

untested but should work.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: December 16, 2007, 08:50:12 PM »

preg_match('/^(d|m|y)[0-9]*?$/'
or
preg_match('/^(d|m|y)[0-9]*?\w/'
I think that's really tight NBs, but I'm wondering about a couplalittle things:

Perhaps should the pattern should look more like this:

$matches = preg_match('/^(d/m/y)[0-9]+$/i');

which would make sure that there is *at least one* digit after the d/m/y and also case-insensitives the match. Also makes sure that the string ends after the digits.

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: December 16, 2007, 08:56:32 PM »

ya true. + makes more sense.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #4 on: December 17, 2007, 03:03:41 AM »

Cheers both, just disected those and they're exactly what I was hoping for - and even better I now know a little more about regular expressions!

Ta

DM
Logged
Dbyt3r
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #5 on: January 14, 2008, 08:29:44 AM »

/^(d|m|y)\w+/


From what I understood, that should do it.
Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: January 14, 2008, 09:20:47 AM »

wouldnt that match
dave
mom
yankee123
etc?
i think he is looking for matches like
d12
m07
m08
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: January 14, 2008, 09:52:16 AM »

You're right NB... I even think that PHP matches the underscore as well for \w (default behavior for \w in most languages)
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Dbyt3r
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #8 on: January 14, 2008, 01:01:55 PM »

/(d|m|y)\d[\w]*/i Cheesy
Logged

No links in signatures please
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #9 on: January 14, 2008, 09:00:57 PM »

/(d|m|y)\d[\w]*/i Cheesy

hmmmmm.... more like....

/^(d|m|y)\d+?$/i

According to the exact specification laid out in the OP.

Not tested....of course!

Cheers,
td

[edit] LOL, this thread should be called "Evolution of a regex" BTW, I just tested that and by my reckoning it's tight![/edit]

« Last Edit: January 14, 2008, 09:09:00 PM by thedarkness » Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Dbyt3r
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #10 on: January 15, 2008, 04:09:44 AM »

LOL ok so I reread the guy's post, obviously, I didn't read it well enough the first time, I was *trying* to match M333D333 Cheesy

so now that I understand his post, or at least I think I do, i think the shortest and most efficient one would be

/^[dmy]\d+/i

 
Logged

No links in signatures please
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #11 on: January 15, 2008, 05:19:48 AM »

/^[dmy]\d+/i

No. That matches on for example m160432_4265425235 you at least need the $ on the end to signify that there can be nothing after the numerals. I'm not sure about the advantages of [dmy] over (d|m|y) but I suspect there is no big difference (could be quite wrong over this). The ungreedy quantifier "?" is probably redundant so we are left with;

/^[dmy]\d+$/i

and

/^(d|m|y)\d+$/i

Can anyone offer a convincing argument about the advantages of one over the other?

Cheers,
td

[edit] Should I even mention
Code:
/^[dmy]{1}\d+$/i
or
Code:
/^(d|m|y){1}\d+$/i
   ? [/edit]

« Last Edit: January 15, 2008, 05:29:46 AM by thedarkness » Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Dbyt3r
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #12 on: January 15, 2008, 07:28:20 AM »

Using ( ) will capture the value of d,m,y and return it, if this regex is applied to a large number of values, it would be better not to use it.

The {1} is not needed since [dmy] will have to find one to carry on.

And about the $, ya sorry forgot that out lol.
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #13 on: January 15, 2008, 07:56:05 AM »

- String must start with either D, M or Y (singular)
- Above must be followed by at least one number
- First condition must only be followed by numbers

preg_match('/^[DMY][0-9]+$/');
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #14 on: January 16, 2008, 12:40:29 AM »

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

Strangely it's amusing the Hell out of me.

Is [0-9] equivalent to "\d" ? Are they interchangeable in all situations?

Keen eye for detail my bro, i didn't notice the capitals in the OP until you mentioned it.

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!