0

Below is a regex which works fine in .net but give me error if I use it in PHP.

(?<=")([^"]+,[^"]+)?(?=")

What does this regex is supposed to return?

Input: 1,"x1",43,"tr","y,7"

It will return me "y,7" in this case. In general, it will return any part between " and " if it has a comma between it.

When I try to use this in PHP, I get following error:

Unknown modifier '('

Please help.

1 Answer 1

2
~(?<=")([^"]+,[^"]+)?(?=")~

For php (preg_* functions) you need to specify delimiter around the regex iteself. It can be !, /, #, ~, etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Can be even (...). That is why PHP throws this error, it thinks the expression is (?<=") and the following ( is not a valid modifier.
Array ( [0] => 1,"x1",43,"tr"," [1] => " ) Array ( [0] => 1,"x1",43,"tr"," [1] => " ) I need "y,7" in result.
@Mihir: debug it, then. Rewrite it from the scratch.
@Mihir: Works fine for me: preg_match_all('~(?<=")([^"]+,[^"]+)?(?=")~','1,"x1",43,"tr","y,7"', $a); You might want the first character group match non-greedy: [^"]+? or add , to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.