使用 ER 验证自定义表达式

发布于 2024-12-29 18:59:01 字数 655 浏览 1 评论 0原文

我正在尝试验证自定义表达式。该表达式包含以下条件:

  • 数字位置;
  • 类型操作;
  • 表达;
  • 别名(可选);
  • 评论(可选);

条件之间用“|”分隔管道。所以,四根管子是极限。

因此,要成为有效的表达式:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2025-01-05 18:59:01

尝试在正则表达式末尾使用 [^|] 而不是 (.+)?

^(\d{1,10})\|(S|M|I|N)\|[^|]+\|([a-zA-Z0-9]*)\|([^|]*)$

作为旁注:对于每个子表达式 subexpr 以下正则表达式是等效的:

((subexpr)+)?

(subexpr)*

Try using [^|] instead of (.+)? at the end of your regex:

^(\d{1,10})\|(S|M|I|N)\|[^|]+\|([a-zA-Z0-9]*)\|([^|]*)$

As a side note: for every sub-expression subexpr the following regexes are equivalent:

((subexpr)+)?

and

(subexpr)*
望她远 2025-01-05 18:59:01

如果它是除管道之外的任何字符,您可以修改正则表达式,以便在末尾添加此规则:

^(\d{1,10})\|(S|M|I|N)\|(.+?)\|([a-zA-Z0-9]+)?\|([^\|]+)?$

If it's any character except the pipe, you can modify the regex so to add this rule at the end:

^(\d{1,10})\|(S|M|I|N)\|(.+?)\|([a-zA-Z0-9]+)?\|([^\|]+)?$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文