RE: [PHP] Regular Expression just one step away from what I need....
[snip] I am no regex expert but wouldn't preg_match_all( "/'([^']+)'/Ui", $theString, $matches); Be more flexible? [/snip] Thanks all, I completely forgot about greedy/ungreedy. That is what you get for being rusty! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression just one step away from what I need....
I am no regex expert but wouldn't preg_match_all( "/'([^']+)'/Ui", $theString, $matches); Be more flexible? On 8/17/07, Thijs Lensselink <[EMAIL PROTECTED]> wrote: > > > If it's only real words this will do: > > $theString = "'foo''bar''glorp'"; > preg_match_all( "/'([a-z]+)'/Ui", $theString, $matches); > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] Regular Expression just one step away from what I need....
On Fri, 2007-08-17 at 12:00 -0500, Jay Blanchard wrote: > Given the string 'foo''bar''glorp' (all quotes are single quotes)I had > hoped to find a regular expression using preg_match that would return an > array containing just those words without having to go through > additional gyrations, like exploding a string to get an array. I have > only had limited luck > > $theString = "'foo''bar''glorp'"; > preg_match( "/\'(.*)\'/", $theString, $matches); > print_r($matches); You want to add the ungreedy modifier... preg_match( "/\'(.*)\'/U", $theString, $matches); Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression just one step away from what I need....
Jay Blanchard wrote: > Given the string 'foo''bar''glorp' (all quotes are single quotes)I had > hoped to find a regular expression using preg_match that would return an > array containing just those words without having to go through > additional gyrations, like exploding a string to get an array. I have > only had limited luck > > $theString = "'foo''bar''glorp'"; > preg_match( "/\'(.*)\'/", $theString, $matches); > print_r($matches); > > Array > ( > [0] => 'foo''bar''glorp' > [1] => foo''bar''glorp > ) > > Of course $matches[0] has the entire string and $matches[1] contains the > returned string minus the leading and trailing single quote. I can > explode $matches[1] to get what I want, but it is an extra step and I am > sure that I have done this before but cannot locate the code in > question. I would like the results to be > > Array > ( > [0] => 'foo''bar''glorp' > [1] => foo > [2] => bar > [3] => glorp > ) > > That way I can use the $matches array without having to create another > array to hold the values. I feel as if I am really close on the regex to > do this, but cannot seem to find (after much head scratching and teeth > gnashing) the proper solution. > > Much thanks! > > If it's only real words this will do: $theString = "'foo''bar''glorp'"; preg_match_all( "/'([a-z]+)'/Ui", $theString, $matches); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression just one step away from what I need....
Given the string 'foo''bar''glorp' (all quotes are single quotes)I had hoped to find a regular expression using preg_match that would return an array containing just those words without having to go through additional gyrations, like exploding a string to get an array. I have only had limited luck $theString = "'foo''bar''glorp'"; preg_match( "/\'(.*)\'/", $theString, $matches); print_r($matches); Array ( [0] => 'foo''bar''glorp' [1] => foo''bar''glorp ) Of course $matches[0] has the entire string and $matches[1] contains the returned string minus the leading and trailing single quote. I can explode $matches[1] to get what I want, but it is an extra step and I am sure that I have done this before but cannot locate the code in question. I would like the results to be Array ( [0] => 'foo''bar''glorp' [1] => foo [2] => bar [3] => glorp ) That way I can use the $matches array without having to create another array to hold the values. I feel as if I am really close on the regex to do this, but cannot seem to find (after much head scratching and teeth gnashing) the proper solution. Much thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php