[英]Regex - Validate Closing tag - Template Engine
For a particular template engine, where user will add/update templates to send messages to customers.对于特定的模板引擎,用户将在其中添加/更新模板以向客户发送消息。
I am using regular expressions to validate each template literals having double tags, a starting <<
and closing >>
tag in a string.我正在使用正则表达式来验证每个具有双标签的模板文字,一个字符串中的起始
<<
和结束>>
标签。
Given is valid template engine. Given 是有效的模板引擎。
Hi <<cName>>, please make your payment of $ <<totAmt>>
for Account number <<accNum>> given by <<agentName>> at this link <<payLink>>
The validity of this link is 30 minutes.
Given are valid
template literals给定的是
valid
的模板文字<<cName>>
<<totAmt>>
<<accNum>>
<<agentName>>
<<payLink>>
Given are invalid
template literals给定的是
invalid
的模板文字<<cName>
<<agentName>>>>>>>>
<cName>
<<<<<payLink>>
<<cName<<>>
Currently give is my workaround.目前给是我的解决方法。
First I get all template literals using given Regex.首先,我使用给定的正则表达式获取所有模板文字。
<.*?>(?!>)
<.*?>(?!>)
Then loop through each literals to validate using given Regex in PHP.然后遍历每个文字以使用 PHP 中的给定正则表达式进行验证。 If any literal is invalid, so given template updated by user is invalid.
如果任何文字无效,则用户更新的给定模板无效。
^<{2}[^<>]+>{2}(?!>)
^<{2}[^<>]+>{2}(?!>)
$is_tpl_valid = true; //template is valid
$template = 'Hi <<cName>>, please make your payment of $ <<totAmt>>
for Account number <<accNum>> given by <agentName>> at this link <<payLink>>
The validity of this link is 30 minutes.';
echo '<br/> --- File: ' . __FILE__ . '#'. __LINE__ . '------- $template -> ' . $template . ' --------------<br/>';
if(preg_match_all('#<.*?>(?!>)#', $template, $matched))
{
echo '<pre>';
print_r($matched);
foreach($matched[0] as $item)
{
if($is_tpl_valid && !preg_match('#^<{2}[^<>]+>{2}(?!>)#', $item))
{
echo '<br/> --- File: ' . __FILE__ . '#'. __LINE__ . '------- $item -> ' . $item . ' --------------<br/>';
$is_tpl_valid = false; //template is invalid
}
}
}
Can we do it in single Regex or any better solution (in JS / PHP) to find invalid template literals.我们可以在单个正则表达式或任何更好的解决方案(在 JS / PHP 中)中找到无效的模板文字。
Thanks in advance提前致谢
To validate tags you can try this pattern要验证标签,您可以尝试这种模式
(?<!<)<<[^<>]+>>(?!>)
(?<!<)
- Match should not be preceded by <
(?<!<)
- 匹配不应以<
<<
- Match <<
<<
- 匹配<<
[^<>]+
- Match anything except <>
[^<>]+
- 匹配除<>
之外的任何内容>>
- Match >>
>>
- 匹配>>
(?!>)
- Match should not be followed by >
(?!>)
- 匹配后不应跟随>
Use the verbs (*SKIP)(*FAIL)
.使用动词
(*SKIP)(*FAIL)
。
This will attempt to match valid tags then FAIL or match everything with the form <+blah>+
that are, in fact, invalid:这将尝试匹配有效的标签,然后失败或匹配具有
<+blah>+
形式的所有内容,实际上是无效的:
$text = '
<<cName>>
<<totAmt>>
<<accNum>>
<<agentName>>
<<payLink>>
<<cName>
<<agentName>>>>>>>>
<cName>
<<<<<payLink>>
<<cName<<>>
';
preg_match_all('/(?<!<)<<\w+>>(?!>)(*SKIP)(*FAIL)|<+[\w<]+>+/', $text, $m);
print_r($m);
Output: Output:
Array
(
[0] => Array
(
[0] => <<cName>
[1] => <<agentName>>>>>>>>
[2] => <cName>
[3] => <<<<<payLink>>
[4] => <<cName<<>>
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.