Regular expression with match for subpattern

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

Moderator: General Moderators

Post Reply
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Regular expression with match for subpattern

Post 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.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Regular expression with match for subpattern

Post 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?
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Re: Regular expression with match for subpattern

Post 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 ;-)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Regular expression with match for subpattern

Post 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?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Regular expression with match for subpattern

Post 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);
Post Reply