Описание
Это регулярное выражение будет делать следующее:
- матча все строки, начинающиеся с
define
и имеют ключ и значение, установленное внутри круглых скобок
- захвата ключа и значения строк, не включая обмоточные котировки
- все ключи и стоимость, которые должны быть обернуты в одинарные или двойные кавычки
- правильная ручка с экранированными кавычками
- избежать сложных случаев края, как:
define('file path', "C:\\windows\\temp\\");
где сбежавший слэш может существовать до закрытия цитаты
Regex
Примечание: используя следующие флаги: случай -независимая, глобальная, многострочная
^define\(\s*(['"])((?:\\\1|(?:(?!\1).))*)\1\s*,\s*(['"])((?:\\\3|(?:(?!\3).))*)\3\s*\);
Захват группы
- захвата группы 0 получает всю строку
- группа захвата 1 получает тип цитаты, окружающий
key
- захвата группы 2 получает в
key
строку внутри котировки
- группа захвата 3 получает тип цитаты, окружающий е
value
- захвата группа 4 получает
value
строку внутри кавычек
Примеры
Live Demo
https://regex101.com/r/oP4sV0/1
Образец текста
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\\" );
Образец Матчи
[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\\
Объяснение
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))
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
; ';'
Является ли регулярное выражение необходимо для всей задачи? Вы можете использовать регулярное выражение, чтобы найти 'define (..)', затем разделите строку между parens и обрезайте ее и т. Д., Чтобы получить нужные значения. –
См. Http: // stackoverflow.com/questions/1352693/how-to-match-a-quoted-string-with-escaped-quotes-in-it – Barmar
@AndyG да, я мог бы, но я хочу узнать больше о том, как использовать регулярное выражение, так что почему я возник вопрос. – manuel