Search code examples
phpregexreturn

Php regex return values within a pattern


I'm looking for a regex to return the values in between Parameter("").

So for 1 + Parameter("MyVariable") + 36 * Parameter("Hello") + NoParameter("Nine") it should return MyVariable and Hello. I already found a way to return the values between quotes, but this includes the NoParameter result as well. Any tips on how to integrate the Parameter() part?

<?php

$string     =   '1 + Parameter("MyVariable") + 36 * Parameter("Hello") + NoParameter("Nine")';

preg_match_all('/"([^"]+)"/', $string, $matches); 

print_r($matches[1]);

?>

Solution

  • You need to match the substring Parameter as desired. See this demo at regex101

    \bParameter\("\K[^"]+
    

    Here is a PHP Demo at eval.in with your updated code sample.
    Of course you can also use a capture group instead of \K.