使用 ER 验证自定义表达式
我正在尝试验证自定义表达式。该表达式包含以下条件:
- 数字位置;
- 类型操作;
- 表达;
- 别名(可选);
- 评论(可选);
条件之间用“|”分隔管道。所以,四根管子是极限。
因此,要成为有效的表达式:
0|S|write(&var1)|alias1|my comment - OK
0|K|write(&var1)|alias1|我的评论 - 失败
1|I|读取(&var2)|alias2| - 好的
1|S|读取(&var1)|| - 好的
2|N|if(&var1 == &var2);read(&var3)|| - 好的
3|S||| - 失败
3|I|write(&var1)|别名 3| - 失败
3|N|write(&var1)|alias1|我的评论| - 失败
我正在使用此 ER 来验证:
^(\d{1,10})\|(S|M|I|N)\|(.+?)\|([a-zA-Z0 -9]+)?\|(.+)?
但我无法验证以管道结尾的表达式。因为,在评论中可以有除管道之外的任何字符......
一些想法?
谢谢
i am trying to validate a custom expression. This expression contains the follow conditions:
- Number position;
- Type operation;
- Expression;
- Alias (optional);
- Comment (optional);
The conditions are separated by "|" pipe. So, four pipes is the limit.
So, to be a valid expression:
0|S|write(&var1)|alias1|my coment - OK
0|K|write(&var1)|alias1|my coment - FAIL
1|I|read(&var2)|alias2| - OK
1|S|read(&var1)|| - OK
2|N|if(&var1 == &var2);read(&var3)|| - OK
3|S||| - FAIL
3|I|write(&var1)|alias 3| - FAIL
3|N|write(&var1)|alias1|my coment| - FAIL
I am using this ER to validate:
^(\d{1,10})\|(S|M|I|N)\|(.+?)\|([a-zA-Z0-9]+)?\|(.+)?
But i can not validate the expression that ends with pipe. Because, in a comment can have any caracter except a pipe...
Some idea??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在正则表达式末尾使用
[^|]
而不是(.+)?
:作为旁注:对于每个子表达式 subexpr 以下正则表达式是等效的:
和
Try using
[^|]
instead of(.+)?
at the end of your regex:As a side note: for every sub-expression subexpr the following regexes are equivalent:
and
如果它是除管道之外的任何字符,您可以修改正则表达式,以便在末尾添加此规则:
If it's any character except the pipe, you can modify the regex so to add this rule at the end: