DangerMouse

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

Pretty new to

regular expression

 s (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.

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

nutballs

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.

perkiset

quote author=nutballs link=topic=652.msg4433#msg4433 date=1197855581

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.

nutballs

ya true. + makes more sense.

DangerMouse

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 expression

 s!

Ta

DM


Perkiset's Place Home   Politics @ Perkiset's