开发者

ANTLR3: Ensuring that character occurs in at least one place

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

fragment CHARACTER : ('a'..'z'|'0开发者_JAVA百科'..'9'|'-'|'&'|'@'|'$'|'%'|'.'|':'); 
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.


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)*
            ;


Changed code to read

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

This seems to solve the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜