How to capture repeating information with regex

41 Views Asked by At

I try to capture all information in a string but only the last one is captured. I start with regex and i need some help.

The string :

correspondent à l'ASIN B00WU8XXXX, mais les attributs suivants sont en conflit avec : product_type (Vendeur : 'APPLIANCE_ACCESSORY' / Amazon : 'CLEANING_AGENT'), ean (Vendeur : '400650819XXXX' / Amazon : '400650819XXXX'), item_package_quantity (Vendeur : '5' / Amazon : '10'

My regex :

.* (.*?) \(Vendeur : \'(.*?)\' \/ Amazon : \'(.*?)\'

I capture only the last block

item_package_quantity : 5 : 10

That i want to capture :

product_type : APPLIANCE_ACCESSORY : CLEANING_AGENT  
ean : 400650819XXXX: 400650819XXXX  
item_package_quantity : 5 : 10

It must be nothing but i turn around :(

1

There are 1 best solutions below

0
The fourth bird On BEST ANSWER

You could start the pattern with the group 1 match for 1 or more word characters (\w+) and use a negated character class [^']* for the values inside the single quotes.

Depending on the delimiter you have to escape the \/

(\w+) \(Vendeur : '([^']*)' \/ Amazon : '([^']*)'

Regex demo | PHP demo

$pattern = '/(\w+) \(Vendeur : \'([^\']*)\' \/ Amazon : \'([^\']*)\'/m';
$s = 'correspondent à l\'ASIN B00WU8XXXX, mais les attributs suivants sont en conflit avec : product_type (Vendeur : \'APPLIANCE_ACCESSORY\' / Amazon : \'CLEANING_AGENT\'), ean (Vendeur : \'400650819XXXX\' / Amazon : \'400650819XXXX\'), item_package_quantity (Vendeur : \'5\' / Amazon : \'10\'';

if(preg_match_all($pattern, $s, $matches, PREG_SET_ORDER)) {
    print_r(array_map(fn($m) => implode(" : ", array_slice($m, 1)), $matches));
}

Output

Array
(
    [0] => product_type : APPLIANCE_ACCESSORY : CLEANING_AGENT
    [1] => ean : 400650819XXXX : 400650819XXXX
    [2] => item_package_quantity : 5 : 10
)