Описание
Попробуйте permlink
[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]
Это будет соответствовать всему тексту внутри вашей {%
и %}
скобки, и будет автоматически обрезать текст, прежде чем положить значение в свои группы.
Группа 0 получает всю строку соответствия
- получает текст Открывающий тег
- Получает внутренний текст
- получает текст конечный тег
Отказ
Это, вероятно, будет иметь некоторые случаи ребер, когда регулярное выражение будет терпеть неудачу, если у вас есть сложные данные, вложенные в sub, если так, то использование regex, вероятно, не будет лучшим инструментом для этой задачи.
Резюме
[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]
Char class [{] matches one of the following chars: {
% Literal `%`
\s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
1st Capturing group ([^}]*start[^}]*)
Negated char class [^}] infinite to 0 times matches any char except: }
start Literal `start`
Negated char class [^}] infinite to 0 times matches any char except: }
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
\s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
% Literal `%`
Char class [}] matches one of the following chars: }
\s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
2nd Capturing group (.*?)
. 0 to infinite times [lazy] Any character (except newline)
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
\s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
Char class [{] matches one of the following chars: {
% Literal `%`
\s infinite to 0 times Whitespace [\t \r\n\f]
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
3rd Capturing group ([^}]*end[^}]*)
Negated char class [^}] infinite to 0 times matches any char except: }
end Literal `end`
Negated char class [^}] infinite to 0 times matches any char except: }
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
\s infinite to 0 times Whitespace [\t \r\n\f]
% Literal `%`
Char class [}] matches one of the following chars: }
PHP пример
с образцом текста {% start %} this is a sample text 1 {% end %}{% start %} this is a sample text 2 {% end %}
<?php
$sourcestring="your source string";
preg_match_all('/[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => {% start %} this is a sample text 1 {% end %}
[1] => {% start %} this is a sample text 2 {% end %}
)
[1] => Array
(
[0] => start
[1] => start
)
[2] => Array
(
[0] => this is a sample text 1
[1] => this is a sample text 2
)
[3] => Array
(
[0] => end
[1] => end
)
)
Есть ли у вас какой-то образец текста? –
В зависимости от языка, lookbehind не может быть переменной длины. – Toto
Вот пример текста: http://pastebin.com/AUX1hd2T Я также обновил свой вопрос с сообщением об ошибке. Я использую PHP. – MarkL