[英]Matching strings without single braces
I am working on regular expression based validation of some user input fields.我正在对一些用户输入字段进行基于正则表达式的验证。 The string that needs to be validated in given below
下面给出需要验证的字符串
{{custName}} With Account Number {{accountnumber}} Your Balance Is {{message_abc.com}}
and the respective regular expression to check the string is given below下面给出了检查字符串的相应正则表达式
[{{[A-Za-z,_.}} ]*
which works absolutely fine for the above mentioned string, but what I want is: if the string is eg:对于上述字符串来说,这绝对可以,但我想要的是:如果字符串是例如:
{custName} With Account Number {accountnumber} Your Balance Is {message_abc.com}
It would give an error.它会给出一个错误。
This matches strings that don't have any single brace:这匹配没有任何单括号的字符串:
^(?:(?!(?<!{){(?!{))(?!(?<!})}(?!}))[{[A-Za-z,_.} ])*$
(??(?<!{){(?!{))
: avoid to match openning brace without another before or after (??(?<!{){(?!{))
:避免在没有另一个之前或之后匹配左大括号(??(?<!})}(?!}))
: avoid to match closing brace without another before or after (??(?<!})}(?!}))
:避免在没有另一个之前或之后匹配右大括号You can match from {{...}}
followed by the character class without the space and without the curly braces.您可以匹配
{{...}}
后跟字符 class 没有空格和大括号。
Then repeat that 0+ as a whole and match at least a single times the {{...}}
part.然后将 0+ 作为一个整体重复并至少匹配一次
{{...}}
部分。
^(?:\{\{[A-Za-z,_.]*}}[A-Za-z,_. ]*)*\{\{[A-Za-z,_.]*}}$
^
Start of string ^
字符串开头(?:
Non capture group (?:
非捕获组
\{\{[A-Za-z,_.]*}}
Match {{...}}}
\{\{[A-Za-z,_.]*}}
匹配{{...}}}
[A-Za-z,_. ]*
[A-Za-z,_. ]*
Optionally repeat any of the listed chars in the character class [A-Za-z,_. ]*
可选择重复字符 class 中列出的任何字符)*
Close the group and repeat 0+ times )*
关闭组并重复 0+ 次\{\{[A-Za-z,_.]*}}
Match at least a single occurrence of {{...}}}
\{\{[A-Za-z,_.]*}}
匹配至少一次出现的{{...}}}
$
End of string $
字符串结尾Regex demo |正则表达式演示| Php demo
Php 演示
If there can also be content before and after:如果前后也可以有内容:
^[A-Za-z,_. ]*(?:\{\{[A-Za-z,_.]*}}[A-Za-z,_. ]*)+[A-Za-z,_. ]*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.