Description
This regex will do the following:
- match all lines that start with
define
and have a key and value set inside parentheses
- capture the key and value strings, without including the wrapping quotes
- all key and value to be wrapped in single or double quotes
- correctly handle escaped quotes
- avoid difficult edge cases like:
define( 'file path', "C:\\windows\\temp\\" );
where an escaped slash may exist before a closing quote
The Regex
Note: using the following flags: case-insensitive, global, multiline
^define\(\s*(['"])((?:\\\1|(?:(?!\1).))*)\1\s*,\s*(['"])((?:\\\3|(?:(?!\3).))*)\3\s*\);

Capture groups
- capture group 0 gets the entire string
- capture group 1 gets the quote type surrounding the
key
- capture group 2 gets the
key
string inside the quotes
- capture group 3 gets the quote type surrounding the
value
- capture group 4 gets the
value
string inside the quotes
Examples
Live Demo
https://regex101.com/r/oP4sV0/1
Sample Text
define("0 My_KEY", "0 My_Value l");
define('1 My_KEY', '1 My_Value');
define( '2 My_KEY' , "2 My_Value" );
define( '3 My_KEY\\' , '3 My\'_\'Value' );
define( '4 My_KEY' , "4 My'_'Value\\" );
Sample Matches
[0][0] = define("0 My_KEY", "0 My_Value l");
[0][1] = "
[0][2] = 0 My_KEY
[0][3] = "
[0][4] = 0 My_Value l
[1][0] = define('1 My_KEY', '1 My_Value');
[1][1] = '
[1][2] = 1 My_KEY
[1][3] = '
[1][4] = 1 My_Value
[2][0] = define( '2 My_KEY' , "2 My_Value" );
[2][1] = '
[2][2] = 2 My_KEY
[2][3] = "
[2][4] = 2 My_Value
[3][0] = define( '3 My_KEY' , '3 My\'_\'Value' );
[3][1] = '
[3][2] = 3 My_KEY\\
[3][3] = '
[3][4] = 3 My\'_\'Value
[4][0] = define( '4 My_KEY' , "4 My'_'Value" );
[4][1] = '
[4][2] = 4 My_KEY
[4][3] = "
[4][4] = 4 My'_'Value\\
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
define 'define'
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
['"] any character of: ''', '"'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
\\ '\'
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
['"] any character of: ''', '"'
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
\\ '\'
----------------------------------------------------------------------
\3 what was matched by capture \3
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\3 what was matched by capture \3
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
\3 what was matched by capture \3
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
; ';'