Michael Fanta
Michael Fanta

Reputation: 31

Regex ignore the delimiter in parentheses

Good morning, I need a little of your help. I need to split the text using REGEX, but to omit the content in parentheses

preg_match_all('/\((?:[^()]|(?R))+\)|\'[^\']*\'|[^(),]+/', $input_lines, $output_array);

I have this string: Test A, Test B, Test C (data1, data1)

And pregmatch we do this:

0   =>  Test A
1   =>   Test B
2   =>   Test C 
3   =>  (data1, data1)

How do I achieve this result?

0   =>  Test A
1   =>  Test B
2   =>  Test C (data1, data1)

I need to ignore the content in parentheses and separate only the rest.

Thank you in advance for any help.

EDIT

This aventually resolved my situation. I tried to use preg split.

preg_split('/,(?![^(]*\)) /', $input_line);

Upvotes: 2

Views: 231

Answers (1)

The fourth bird
The fourth bird

Reputation: 163277

What you might do is use the recursive pattern to recurse the first subpattern using (?1) to match all the parenthesis so the split does not split inside the parenthesis as well and use SKIP FAIL.

Then split on a comma followed by 0+ horizontal whitespace chars

(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,\h*

Regex demo | Php demo

$re = '/(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,\h*/';
$strings = [
    "Test A, Test B, Test C (data1, data1)",
    "Test A, Test B, Test C (data1, data1), Test D (data1, data1), Test E (data1, data1(data, data))",
    "Test A, Test B, Test C (data1, data1), Test D (data1, data1), Test E ((data1, data1))"
];

foreach($strings as $s) {
    print_r(preg_split($re, $s));
}

Output

Array
(
    [0] => Test A
    [1] => Test B
    [2] => Test C (data1, data1)
)
Array
(
    [0] => Test A
    [1] => Test B
    [2] => Test C (data1, data1)
    [3] => Test D (data1, data1)
    [4] => Test E (data1, data1(data, data))
)
Array
(
    [0] => Test A
    [1] => Test B
    [2] => Test C (data1, data1)
    [3] => Test D (data1, data1)
    [4] => Test E ((data1, data1))
)

Upvotes: 2

Related Questions