Page 1 of 1

Regular expression with match for subpattern

Posted: Thu Sep 11, 2008 5:33 am
by jollyjumper
Hi Everyone,

I've got the following code

Code: Select all

 
<?php
$key = 'S,M,L,XL-A/B,C/D';
if(preg_match_all("#((\w{1,2}),?)+-((\w/\w),?)+#", $key, $matches)){
    echo '<pre>';
    print_r($matches);
}
?> 
 
which results in:

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => S,M,L,XL-A/B,C/D
        )
 
    [1] => Array
        (
            [0] => XL
        )
 
    [2] => Array
        (
            [0] => XL
        )
 
    [3] => Array
        (
            [0] => C/D
        )
 
    [4] => Array
        (
            [0] => C/D
        )
 
)
 
It returns the last match of every subpattern. What I wanted to achieve is getting al the possible matches, something like:

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => S,M,L,XL-A/B,C/D
        )
 
    [1] => Array
        (
            [0] => S
            [1] => M
            [2] => L
            [3] => XL
        )
 
    [2] => Array
        (
            [0] => S,
            [1] => M,
            [2] => L,
            [3] => XL
        )
 
    [3] => Array
        (
            [0] => A/B
            [1] => C/D
        )
 
    [4] => Array
        (
            [0] => A/B,
            [1] => C/D
        )
 
) 
 
Is this possible and could you tell me how?

thanks in advance.

Re: Regular expression with match for subpattern

Posted: Thu Sep 11, 2008 6:31 am
by marcth
What exactly are you trying to do? From what I understand you have a string:

$key = 'S,M,L,XL-A/B,C/D';

and you want to split it up into it's atomic/sub-atomic parts. What is the algorithm?

Re: Regular expression with match for subpattern

Posted: Thu Sep 11, 2008 7:15 am
by jollyjumper
Hi marcth,

Thanks for your reply.

I don't think there is a real algorithm. What I want is really simple.

I've got a excel sheet with different products and one column contains strings like S,M,L,XL-A/B,C/D (it are sizes of a product)

The different sizes that this string contains are:

S
M
L
XL
A/B
C/D

Without a regex I propably would explode the string on the - first and after that explode both the array indexes on the ,
Then of course turn it in one array, or something similair, as long as I can call apart the different sizes per product.

Does this make some sense or is it still one messy story ;-)

Re: Regular expression with match for subpattern

Posted: Thu Sep 11, 2008 9:10 pm
by marcth
jollyjumper wrote:I don't think there is a real algorithm. What I want is really simple.

I've got a excel sheet with different products and one column contains strings like S,M,L,XL-A/B,C/D (it are sizes of a product)

The different sizes that this string contains are:

S
M
L
XL
A/B
C/D
I apologize because I'm still confused. Do you want to transform the above list into and array as you detailed as sizes? If so, we can help.

Edit: Are you doing a data migration?

Re: Regular expression with match for subpattern

Posted: Fri Sep 12, 2008 6:33 am
by GeertDD
jollyjumper wrote:Without a regex I propably would explode the string on the - first and after that explode both the array indexes on the ,
Then of course turn it in one array, or something similair
What you don't know is that you can do that with a regex too. :)

Code: Select all

preg_split('~[-,\s]+~', $codes);