#regex
Вопрос:
Я пытаюсь найти несколько совпадений в определенной позиции в тексте.
Например, в следующем тексте я хочу найти все a=.*;
между abcd
и xyz
abcd
a=1;
some text
a=2; some text
b=3;
a=4;
xyz
a=5;
a=6;
Так что он должен соответствовать только:
a=1;
a=2;
a=4;
и не совпадают:
a=5;
a=6;
До сих пор я пробовал следующее регулярное выражение:
(?<=abcdn)(.*)(?=nxyz)
который возвращает строку между 'abcd'
и 'xyz'
,но мне не удается сопоставить все a=.*;
внутри
Ответ №1:
Воспользуйся
(?<=abcd[sS]*)a=.*;(?=[sS]*xyz)
См.Доказательство регулярного выражения JS.
объяснение
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
abcd 'abcd'
--------------------------------------------------------------------------------
[sS]* any character of: whitespace (n, r,
t, f, and " "), non-whitespace (all
but n, r, t, f, and " ") (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
a= 'a='
--------------------------------------------------------------------------------
.* any character except n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[sS]* any character of: whitespace (n, r,
t, f, and " "), non-whitespace (all
but n, r, t, f, and " ") (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
xyz 'xyz'
--------------------------------------------------------------------------------
) end of look-ahead
Ответ №2:
простой: a=.*;
Группа: (a)=(.*);
Q: a(?==.*;)
A: (?<=a=).*(?=;)
Комментарии:
1. В вопросе говорится
I want to find all a=.*; between abcd and xyz
Шаблоны, которые вы предоставили, не только дают совпадения подSo it only match: