打印 lex 中除最后一个字符外的匹配字符串

Print the matched string except the last character in lex

我很困惑如何解决以下问题。假设我在 lex 中匹配 'xyz!' 形式的字符串,但现在我只想打印 'xyz' 字符串,它不包括初始匹配字符串的最后一个字符。我知道如何打印匹配的字符串,

printf("String:%s", yytext)

但不确定如何只打印字符串 'xyz'。有人可以澄清这个问题吗?提前致谢!

printf("String:%.*s", yyleng - 1, yytext);

* 在 printf 格式中通常意味着 "get the numeric value from the next argument, which must be an int. So if yyleng (which is the token length) were 4, then printf would do the equivalent of printf("String:%.3s", yytext), which would mean " 从 yytext.

中打印最多 3 个字符

有关更多详细信息,请参阅 man printf