regexregex-group

Ignore string containing


Using PCRE2 regex for PHP script replacement

The purpose of this regex is to add ?? '' to a strlen (PHP) function call if it does not currently exist. I plan to use a regex capture group to perform replacements in VS Code using strlen($1 ?? ''). The following text string is used for testing the regex:

$strStatement2 = substr($strStatement3, strlen('my capture group'));  # want a match on `strlen('my capture group')` with a capturing group of `'my capture group'`

$strStatement2 = substr($strStatement3, 0, strlen('my capture group') - 1);  # want a match on `strlen('my capture group')` with a capturing group of `'my capture group'`

$strStatement2 = strlen($doNotWantAMatch ?? '') != strlen('my capture group');  # do not want a match on the first instance of `strlen` since it contains ` ?? ''` but I do want to match the second instance of `strlen('my capture group')`

If I try regex of strlen\((?:(?! ?? '')(.))+\) (assuming the use of /[regex here]/gm) I get matches on the following (after removing the comments from the end of the lines):

$strStatement2 = substr($strStatement3, strlen('my capture group'));

$strStatement2 = substr($strStatement3, 0, strlen('my capture group') - 1);

$strStatement2 = strlen($doNotWantAMatch ?? '') != strlen('my capture group');

This is good because it at least ignores strlen($doNotWantAMatch ?? ''), but the other matches are not correct.

The basic regex I need is strlen\((.+?)\) which forms the capture group for the argument within the strlen function I am trying to replace. A regex of strlen\((?:(?! ?? '')(.+?))\) gets me close as it gives the cature groups, except it fails to ignore a match and capture group for strlen($strStatement3 ?? ''). If I use strlen\(.*?(?: \?\? '')\)|strlen\((.+?)\) I get the capture groups I want (in that it does not create a capture group for strlen($strStatement3 ?? '')) but I do not want the match on strlen($doNotWantAMatch ?? '') (which is being caused by the 'or' |, but by this point I am grasping at straws).

I feel I am close to a solution but I am unable to get across the finish line on this problem. Using https://regex101.com/r/HRWt8Q/1 for testing.

Update

@anubhava provided the answer and I ended up using strlen\(((?:[^)](?!\?\? ''))+)\) in VSCode to search for instances of strlen() within PHP scripts that needed to default to an empty string for PHPv8.1. if a null value is passed in (since passing a null string to strlen() is now deprecated). This helped me update my old PHP code to PHPv8.1 where many functions need to have something other than null passed into them.


Solution

  • You may use this regex to capture text between strlen( and ) that must not contain ??:

    strlen\(((?:[^)](?!\?\? ''))+)\)
    

    RegEx Demo

    RegEx Details: