Validating the inside of an array() function

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Validating the inside of an array() function

Post by Ambush Commander »

I can't see how this would be practically applied, but it involves some cool magic. Is it okay if I define the $ assertion of the end of a line twice?

Code: Select all

static $regexp = 
'/^(?#				Assert start of Array Declaration
)(?:(?#				Begin Comma Indented Sequence - Last sequence
				wont have an ending comma
)ї\s]*(&quote;|\')ї^\1]*\1ї\s]*(?# 	Initial Quote Check. SubPattern 1 Used
)(?:=>(?# 			Arrow Sign. May not exist. If it does, parse a second one
)ї\s]*(&quote;|\')ї^\2]*\2ї\s]*)(?# 	End Second Quote Declaration
)(?:,ї\s]*|$)(?# 		Add a comma or assert the end of the pattern
))*(?# 				Continue Loop if end of line wasnt asserted
)$/';//				Assert end of Regular Expression
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may want to edit this to work as code, with the comments... as it stands, it is not.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, it works. I made sure that the comments didn't break anything. Makes the regular expression more readable.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no.. it actually doesn't. The "comments" are stored inside the string.

Code: Select all

<?php

static $regexp = 
'/^(?#				Assert start of Array Declaration
)(?:(?#				Begin Comma Indented Sequence - Last sequence
				wont have an ending comma
)ї\s]*(&quote;|\')ї^\1]*\1ї\s]*(?# 	Initial Quote Check. SubPattern 1 Used
)(?:=>(?# 			Arrow Sign. May not exist. If it does, parse a second one
)ї\s]*(&quote;|\')ї^\2]*\2ї\s]*)(?# 	End Second Quote Declaration
)(?:,ї\s]*|$)(?# 		Add a comma or assert the end of the pattern
))*(?# 				Continue Loop if end of line wasnt asserted
)$/';//				Assert end of Regular Expression

echo $regexp;

?>

Code: Select all

/^(?#                           Assert start of Array Declaration
)(?:(?#                         Begin Comma Indented Sequence - Last sequence
                                wont have an ending comma
)ї\s]*(&quote;|')ї^\1]*\1ї\s]*(?#     Initial Quote Check. SubPattern 1 Used
)(?:=>(?#                       Arrow Sign. May not exist. If it does, parse a second one
)ї\s]*(&quote;|')ї^\2]*\2ї\s]*)(?#    End Second Quote Declaration
)(?:,ї\s]*|$)(?#                Add a comma or assert the end of the pattern
))*(?#                          Continue Loop if end of line wasnt asserted
)$/

Code: Select all

/^(?#                           Assert start of Array Declaration
)(?:(?#                         Begin Comma Indented Sequence - Last sequence
                                wont have an ending comma
)ї\s]*(&quote;|')ї^\1]*\1ї\s]*(?#     Initial Quote Check. SubPattern 1 Used
)(?:=>(?#                       Arrow Sign. May not exist. If it does, parse a second one
)ї\s]*(&quote;|')ї^\2]*\2ї\s]*)(?#    End Second Quote Declaration
)(?:,ї\s]*|$)(?#                Add a comma or assert the end of the pattern
))*(?#                          Continue Loop if end of line wasnt asserted
)$/
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh, yeah, the comments are still in the string. But they're valid comments for preg_match

Code: Select all

(?# This is a comment in a regular expression)
Try testing it out.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my point is there is no reason for them to be in there.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh... Okay...

Code: Select all

$regexp = 
'/^(?:[\s]*("|\')[^\1]*\1[\s]*(?:=>[\s]*("|\')[^\2]*\2[\s]*)(?:,[\s]*|$))*$/';
//Ugh... the PHP thing keeps folding over.
//
//
Post Reply