ANTLR3:确保该字符至少出现在一个地方

发布于 2024-11-14 20:47:58 字数 487 浏览 4 评论 0原文

我试图确保带有通配符的文本中至少包含一个字符,

fragment CHARACTER : ('a'..'z'|'0'..'9'|'-'|'&'|'@'|'$'|'%'|'.'|':'); 
fragment WILDCARD_QUESTION  : ('?'); 

WILD_STRING 
   : (CHARACTER)* 
     ( 
       (WILDCARD_QUESTION) 
       (CHARACTER)* 
     )+;

即使使用一个?在我希望失败的地方被接受。因此,仅应传递至少一个字符的序列。

我需要的是 a??a?a?a?a 等通过。只有 ??? 等应该失败。即应该至少有一个字符而不仅仅是WILDCARD_QUESTION。该字符可以位于通配符的任一侧。

I am trying to ensure that at least one character is included in the text with wildcards

fragment CHARACTER : ('a'..'z'|'0'..'9'|'-'|'&'|'@'|'

Using this even a single ? gets accepted where as I would like it to fail. So only sequences that that at least one character should be passed.

What I need is for a?, ?a, ?a?, a?a etc to pass. Only ?, ?? etc should fail. ie there should be at least one character and not just WILDCARD_QUESTION. The character can be on either side of the wildcard.

|'%'|'.'|':'); fragment WILDCARD_QUESTION : ('?'); WILD_STRING : (CHARACTER)* ( (WILDCARD_QUESTION) (CHARACTER)* )+;

Using this even a single ? gets accepted where as I would like it to fail. So only sequences that that at least one character should be passed.

What I need is for a?, ?a, ?a?, a?a etc to pass. Only ?, ?? etc should fail. ie there should be at least one character and not just WILDCARD_QUESTION. The character can be on either side of the wildcard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

掩饰不了的爱 2024-11-21 20:47:58

使用两条规则来执行此操作,一条用于前导通配符,另一条用于前导字符:

WILD_STRING : (WILDCARD_QUESTION)+ CHARACTER (CHARACTER | WILDCARD_QUESTION)*
            | (CHARACTER)+ WILDCARD_QUESTION (CHARACTER | WILDCARD_QUESTION)*
            ;

Do it with two rules, one for leading WILDCARDs and one for leading CHARACTERs:

WILD_STRING : (WILDCARD_QUESTION)+ CHARACTER (CHARACTER | WILDCARD_QUESTION)*
            | (CHARACTER)+ WILDCARD_QUESTION (CHARACTER | WILDCARD_QUESTION)*
            ;
彻夜缠绵 2024-11-21 20:47:58

更改代码以读取

    WILD_STRING 
       : (((WILDCARD_QUESTION)+(CHARACTER))|((CHARACTER)+(WILDCARD_QUESTION)))
(WILDCARD_QUESTION|CHARACTER)*
       ; 

这似乎可以解决问题。

Changed code to read

    WILD_STRING 
       : (((WILDCARD_QUESTION)+(CHARACTER))|((CHARACTER)+(WILDCARD_QUESTION)))
(WILDCARD_QUESTION|CHARACTER)*
       ; 

This seems to solve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文