Page 1 of 2

A simple REGEX please

Posted: Fri Jul 09, 2004 3:03 am
by JayBird
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

Posted: Fri Jul 09, 2004 3:54 am
by markl999
Maybe...

Code: Select all

if(preg_match('|^V[0-9]+/P[0-9]+$|', $string)){
  echo 'Ok';
} else {
  echo 'Not Ok';
}

Posted: Fri Jul 09, 2004 3:55 am
by JayBird
thanks, but i did this, which is best?

Code: Select all

function checkPrintRef($string) {
	if(ereg("V[0-9]+/P[0-9]+", $string)) {
		echo "correct format";
	} else {
		echo "wrong format";
	}
}
Mark

Posted: Fri Jul 09, 2004 3:57 am
by markl999
$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*

Posted: Fri Jul 09, 2004 3:58 am
by JayBird
ah, good thinking

is there any difference for me using ereg or preg_match?

Mark

Posted: Fri Jul 09, 2004 3:59 am
by markl999
PCRE is 'faster' than POSIX iirc

Posted: Fri Jul 09, 2004 4:00 am
by JayBird
okay, ill go for your methd then

can you just tell me what the bold bits do please

|^V[0-9]+/P[0-9]+$|

Mark

Posted: Fri Jul 09, 2004 4:03 am
by markl999
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>#'

Posted: Fri Jul 09, 2004 4:04 am
by JayBird
so whats the final dollar sign for?

Mark

Posted: Fri Jul 09, 2004 4:08 am
by markl999
Oh sorry, missed that bold bit.
In regex ^ means 'beginning of string' and $ means 'end of string',
so ^foo$ means '<beginning of string>foo<end of string>'

(note: the ^ and $ can change their meaning depending on the context, eg inside a character class [] ^ means 'not', so [^f] means 'not f')

Posted: Fri Jul 09, 2004 4:11 am
by JayBird
ah, nice one, thanks Mark.

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 :)

Cheerz

Mark

Posted: Fri Jul 09, 2004 4:20 am
by markl999
I recommend Mastering Regular Expressions if you seriously want to get into them, it's the bible of regex ;)

Posted: Fri Jul 09, 2004 5:33 am
by redmonkey
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 :)
Yes, you will!

Here is a regex from a Perl module.....

Code: Select all

(?:(?:\r\n)?&#1111; \t])*(?:(?:(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t]
)+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:
\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(
?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; 
\t]))*"(?:(?:\r\n)?&#1111; \t])*))*@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\0
31]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\
](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+
(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:
(?:\r\n)?&#1111; \t])*))*|(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z
|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)
?&#1111; \t])*)*\<(?:(?:\r\n)?&#1111; \t])*(?:@(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\
r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111;
 \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)
?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t]
)*))*(?:,@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111;
 \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*
)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t]
)+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*)
*:(?:(?:\r\n)?&#1111; \t])*)?(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+
|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r
\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:
\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t
]))*"(?:(?:\r\n)?&#1111; \t])*))*@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031
]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](
?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?
:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?
:\r\n)?&#1111; \t])*))*\>(?:(?:\r\n)?&#1111; \t])*)|(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?
:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?
&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)*:(?:(?:\r\n)?&#1111; \t])*(?:(?:(?:&#1111;^()<>@,;:&quote;.\&#1111;\] 
\000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|
\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>
@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"
(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*))*@(?:(?:\r\n)?&#1111; \t]
)*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:\\
".\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?
:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;
\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*|(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-
\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(
?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)*\<(?:(?:\r\n)?&#1111; \t])*(?:@(?:&#1111;^()<>@,;
:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;
^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;
.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\
]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*(?:,@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\
&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\
r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] 
\000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]
|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*)*:(?:(?:\r\n)?&#1111; \t])*)?(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \0
00-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\
.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,
;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?
:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*))*@(?:(?:\r\n)?&#1111; \t])*
(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.
\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;
^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]
]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*\>(?:(?:\r\n)?&#1111; \t])*)(?:,\s*(
?:(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:\\
".\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(
?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;
\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t
])*))*@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t
])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?
:\.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|
\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*|(?:
&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\
]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)*\<(?:(?:\r\n)
?&#1111; \t])*(?:@(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"
()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)
?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>
@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*(?:,@(?:(?:\r\n)?&#1111;
 \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,
;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:\r\n)?&#1111; \t]
)*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:\\
".\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*)*:(?:(?:\r\n)?&#1111; \t])*)?
(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.
\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])*)(?:\.(?:(?:
\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z|(?=&#1111;\&#1111;
"()<>@,;:&quote;.\&#1111;\]]))|"(?:&#1111;^"\r\\]|\\.|(?:(?:\r\n)?&#1111; \t]))*"(?:(?:\r\n)?&#1111; \t])
*))*@(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])
+|\Z|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*)(?:\
.(?:(?:\r\n)?&#1111; \t])*(?:&#1111;^()<>@,;:&quote;.\&#1111;\] \000-\031]+(?:(?:(?:\r\n)?&#1111; \t])+|\Z
|(?=&#1111;\&#1111;"()<>@,;:&quote;.\&#1111;\]]))|\&#1111;(&#1111;^\&#1111;\]\r\\]|\\.)*\](?:(?:\r\n)?&#1111; \t])*))*\>(?:(
?:\r\n)?&#1111; \t])*))*)?;\s*)

Posted: Fri Jul 09, 2004 5:36 am
by JayBird
JESUS WEPT, what is that supposed to do!?

Mark

Posted: Fri Jul 09, 2004 5:42 am
by redmonkey
It is used to validate email addresses according to the RFC 822 grammar.