I use flex, the linux/unix not the Adobe type, to generate small scanners. In the past I have always used static search strings. I now want to provide a command line provided search string by providing a string via getopt and then being able to use it for searching with.
我使用flex,linux / unix而不是Adobe类型来生成小型扫描仪。在过去,我一直使用静态搜索字符串。我现在想通过getopt提供一个字符串提供一个命令行提供的搜索字符串,然后能够用它进行搜索。
The old way of searching was:
旧的搜索方式是:
.*"_"\n ECHO;
To find lines that ended with an underscore.
查找以下划线结尾的行。
Now I want to search this way:
现在我想以这种方式搜索:
.*<arbitrary string>.*\n ECHO;
I don't know how to get flex to accept the <arbitrary string>
. I can get it via getopt, but I haven't been able to get flex to accept my syntax.
我不知道如何让flex接受 <任意字符串> 。我可以通过getopt获得它,但我无法灵活接受我的语法。
What I am doing is a special purpose very limited grep for a special problem I am having.
我正在做的是一个特殊目的非常有限的grep我遇到的一个特殊问题。
Any help would be appreciated.
任何帮助,将不胜感激。
1 个解决方案
#1
1
.*\n { if(strstr(yytext, "arbitrary string")) ECHO; else REJECT; }
The REJECT statement will skip to next rule if yytext doesn't contain "arbitrary string". This will of course not provide the same performance as if the search string was known at compile time. regcomp()/regexec() in glibc might be faster than flex if you are implementing your own grep program.
如果yytext不包含“任意字符串”,则REJECT语句将跳到下一个规则。这当然不会提供与在编译时知道搜索字符串相同的性能。如果要实现自己的grep程序,glibc中的regcomp()/ regexec()可能比flex更快。
#1
1
.*\n { if(strstr(yytext, "arbitrary string")) ECHO; else REJECT; }
The REJECT statement will skip to next rule if yytext doesn't contain "arbitrary string". This will of course not provide the same performance as if the search string was known at compile time. regcomp()/regexec() in glibc might be faster than flex if you are implementing your own grep program.
如果yytext不包含“任意字符串”,则REJECT语句将跳到下一个规则。这当然不会提供与在编译时知道搜索字符串相同的性能。如果要实现自己的grep程序,glibc中的regcomp()/ regexec()可能比flex更快。