PHP Regex to match lines with all-caps with occaisional hyphens.
Posted
by Yaaqov
on Stack Overflow
See other posts from Stack Overflow
or by Yaaqov
Published on 2010-04-20T13:05:27Z
Indexed on
2010/04/21
0:53 UTC
Read the original article
Hit count: 298
I'm trying to to convert an existing PHP Regular Expression match case to apply to a slightly different style of document.
Here's the original style of the document:
**FOODS - TYPE A**
___________________________________
**PRODUCT**
1) Mi Pueblito Queso Fresco Authentic Mexican Style Fresh Cheese;
2) La Fe String Cheese
**CODE**
Sell by date going back to February 1, 2009
And the successfully-running PHP Regex match code that only returns "true" if the line is surrounded by asterisks, and stores each side of the "-" as $m[1] and $m[2], respectively.
if ( preg_match('#^\*\*([^-]+)(?:-(.*))?\*\*$#', $line, $m) ) {
// only for **header - subheader** $m[2] is set.
if ( isset($m[2]) ) {
return array(TYPE_HEADER, array(trim($m[1]), trim($m[2])));
}
else {
return array(TYPE_KEY, array($m[1]));
}
}
So, for line 1: $m[1] = "FOODS" AND $m[2] = "TYPE A"; Line 2 would be skipped; Line 3: $m[1] = "PRODUCT", etc.
The question: How would I re-write the above regex match if the headers did not have the asterisks, but still was all-caps, and was at least 4 characters long? For example:
FOODS - TYPE A
___________________________________
PRODUCT
1) Mi Pueblito Queso Fresco Authentic Mexican Style Fresh Cheese;
2) La Fe String Cheese
CODE
Sell by date going back to February 1, 2009
Thank you.
© Stack Overflow or respective owner