[SOLVED] A simple REGEX please
Moderator: General Moderators
A simple REGEX please
I need to make sure that the user has entered information in a certain format.
the format is:
V#/P#
So it could be
V1/P1
V21/P5
V4/P11
ect...
Thanks
Mark
the format is:
V#/P#
So it could be
V1/P1
V21/P5
V4/P11
ect...
Thanks
Mark
Maybe...
Code: Select all
if(preg_match('|^V[0-9]+/P[0-9]+$|', $string)){
echo 'Ok';
} else {
echo 'Not Ok';
}thanks, but i did this, which is best?
Mark
Code: Select all
function checkPrintRef($string) {
if(ereg("V[0-9]+/P[0-9]+", $string)) {
echo "correct format";
} else {
echo "wrong format";
}
}$string = 'cV1/P1'; will pass your test and not mine, yours is just missing the ^ ... $ delimiters.
if(ereg("^V[0-9]+/P[0-9]+$", $string)) {
And i think perl regex is faster than posix *shrug*
if(ereg("^V[0-9]+/P[0-9]+$", $string)) {
And i think perl regex is faster than posix *shrug*
Last edited by markl999 on Fri Jul 09, 2004 4:18 am, edited 2 times in total.
They are just the regex 'start' and 'end' markers, you can use any character you want really.
I used to always use '/<regex here>/' but it can get ugly if the regex also has slashes in it, so i've now plumped for '|<regex here>|' , though i'd change that if the regex has |'s in it. Another popular one is '#<regex here>#'
I used to always use '/<regex here>/' but it can get ugly if the regex also has slashes in it, so i've now plumped for '|<regex here>|' , though i'd change that if the regex has |'s in it. Another popular one is '#<regex here>#'
I recommend Mastering Regular Expressions if you seriously want to get into them, it's the bible of regex 
Yes, you will!Bech100 wrote:doesn't seem as tricky as i first thought all theis REGEX stuff. It looks complicated, but it really isn't.
I'll probably eat my own words when i get onto more complex pattern matching
Here is a regex from a Perl module.....
Code: Select all
(?:(?:\r\n)?ї \t])*(?:(?:(?:ї^()<>@,;:"e;.\ї\] \000-\031]+(?:(?:(?:\r\n)?ї \t]
)+|\Z|(?=ї\ї"()<>@,;:"e;.\ї\]]))|"(?:ї^"\r\\]|\\.|(?:(?:\r\n)?ї \t]))*"(?:(?:
\r\n)?ї \t])*)(?:\.(?:(?:\r\n)?ї \t])*(?:ї^()<>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