简体   繁体   English

在c中使用正则表达式用于strcmp函数

[英]Use of regular expressions in c for strcmp function

So, what im interesting in trying to do is read in a line with getline and look for specific phases if i see something like "verbose on" or "verbose off" i know what i want to do, however if it is "verbose "something"" then i want to error. 所以,尝试做的有趣的事情是在getline中读取并查找特定阶段,如果我看到“详细打开”或“详细关闭”我知道我想要做什么,但是如果它是“详细” “”然后我想要错误。 Im pretty sure that this is going to require regular expressions because what comes after is arbitrary. 我很确定这将需要正则表达式,因为之后发生的是任意的。 Some insight on this problem would be much appreciated. 对此问题的一些见解将非常感激。 Thank you. 谢谢。

strcmp(buf,"verbose on")==0
strcmp(buf,"verbose off")==0
strcmp(buf,"verbose "regex expression here im thinking"")==0

This is how i think it should go, just need a bit of a push. 这就是我认为它应该去的方式,只需要一点推动。

No need for a regex. 不需要正则表达式。 You can use strncmp : 你可以使用strncmp

strncmp(buf, "verbose", strlen("verbose")) == 0

This only compares the first 7 characters, so it will match any buf that starts with "verbose". 这只比较前7个字符,因此它将匹配以“verbose”开头的任何buf

Note : I'm allergic against magic numbers, but you could of course replace the strlen call with a literal 7 if you prefer. 注意 :我对魔法数字过敏,但如果您愿意,您当然可以用文字7替换strlen调用。 Also, for real code, I would replace the duplicated string literal with a constant. 另外,对于实际代码,我会用常量替换重复的字符串文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM