trying to seperate a list of values with regular expressions

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

trying to seperate a list of values with regular expressions

Post by amir »

Hello,
I'm trying to build a method on my mysql object that will allow me to pass in a associated array of columns and values. I'm at the part where a value might have a few items to it. I need to clean each one and then insert it.

Here is an example of what I mean
passed in value:

Code: Select all

METHOD_1('string',number,METHOD_2(number,'string'))
I want to be able to split the contents of METHOD_1 into its three parts

Code: Select all

'string'
number
METHOD_2(number,'string')
I can't seem to create the regular expression that will allow it to ignore commas inside parentheses.

This is what I have so far

Code: Select all

<?
$string = "'this where it gets sticky', something('here is', 7897,'this is a test') , any('here is another')";

$pattern = "/([a-zA-Z_]*[\(]?[\)]?)(.*?)\\1(?:,|$)/";

preg_match_all($pattern,$string,$matches);

echo $string."<br/>";
echo "<pre>";
print_r($matches);
echo "</pre>";

?>
This is what I get

Code: Select all

Array
(
    [0] => Array
        (
            [0] => 'this where it gets sticky',
            [1] =>  something('here is',
            [2] =>  7897,
            [3] => 'this is a test') ,
            [4] =>  any('here is another')
            [5] =>
        )

    [1] => Array
        (
            [0] =>
            [1] =>
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [2] => Array
        (
            [0] => 'this where it gets sticky'
            [1] =>  something('here is'
            [2] =>  7897
            [3] => 'this is a test')
            [4] =>  any('here is another')
            [5] =>
        )

)
Can someone please help me create the right regular expression to make this work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Regular expressions don't handle recursion well.

How is this going to be used?
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Thanks for your reply.

I want to use this to strip all the values out of a method or any text passed in. The regular expression doesn't need to do recursion. It just needs to split the string apart.

Say I have something that looks like this

Code: Select all

UCASE('this isn't some text')
I want to pull the text out, check it for 's and put it back. So say I pass in something like this

Code: Select all

METHOD('here is some text','here isn't some, more text',METHOD_1('here is some, more text',789757))
There are two spots I need to check. So I want to take the text out of the method and check each spot. An easy way would be to split it at the comma but that will interrupt the second string. Pulling the inside of the method out is easy. Breaking it up is hard.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Two functions, one nested in the other requires recursion.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

...or a stack. Both is not available for regular expressions.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

I have a php function that will handle the recursion. At this point I need to break the string at commas outside of parentheses.
A great many thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You need to build a string parser.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

So you don't think it can be done with a regular expression?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Post Reply