日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用flex 做关键词、正则表达式过滤

發(fā)布時間:2025/3/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用flex 做关键词、正则表达式过滤 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

編寫 .l文件

%{

c 頭文件 和變量定義

%}

?

%%

{pattern} {c action code;}

?

%%

c function definition

?

compile && link

flex -o my.c my.l

gcc -o my my.c -lfl

?

當(dāng)無main函數(shù)時, 需要 -lfl && %option noreject noyywrap

?

?

?

中文支持:

.l 文件與需要檢測的數(shù)據(jù)文件是同一編碼, 否則識別不出來

?

?

vim securecrt centos 編輯顯示中文:

centos?? 編輯i18n文件, 設(shè)置utf8

securecrt 設(shè)置成utf8

~/.vimrc: set encoding=utf8??????? &&????????? set termencoding=utf8

?

====================================================================

example:

#!/bin/shcat keyword | gawk 'BEGIN{print "%%";} {#check if need ""if(index($1, "/"") == 0)print "/"" $0 "/" {";elseprint $0 " {";print "/tprintf(/"found: %s, lineno: %d, length: %d//n/"" ", yytext, yylineno, yyleng);";print "/tstrcpy(pat, yytext);";print "/tyyret = 1;";print "/tYY_FLUSH_BUFFER;/*do not resume scan remain buffer. */";print "/treturn 1;/*1 will be returned by yylex *//n}/n/n"; } END{print ".|//n {/* eat up any unmatched. do nothing */}/n/n";print "<<EOF>> {";print "/treturn 0;/n}/n/n" }' > kk && cat head kk tail > k2.l make

?

makefile:

?# Basic Makefile -- provides explicit rules
# Creates "myprogram" from "scan.l" and "myprogram.c"
#
CC = g++

CFLAGS = -g
LDFLAGS = #-lfl, can use this if && only if no main provided,

LEX=flex
check: k2.o check.o
?$(CC) -o $@? $(LDFLAGS) $^

check.o: check.cpp
?$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^

k2.o: k2.cpp
?$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^

k2.cpp: k2.l
?$(LEX) $(LFLAGS) -o $@ $^

clean:
?$(RM) *.o k2.cpp

?

?

check.cpp: test code

#include <stdio.h> #include <time.h>extern int check2(FILE *f, char *fn, char *pattern, FILE *out, int *ret, int output);int main(int argc, char *argv[]) {int ret;int yret = 0;clock_t start, end;unsigned int all;unsigned int found = 0, notfound = 0;FILE *out = fopen("/dev/null", "a");char pattern[1024];int i = 0, j = 0;char *prefix = "../../../sample_mail/spam_test/spam.";char path[200];for(i = 1; i < 10001; i++){sprintf(path, "%s%d", prefix, i);FILE * f = fopen(path, "r");if(f != 0){start = clock();for(j = 0; j < 10; j++){fseek(f, 0, SEEK_SET);pattern[0] = 0;ret = check2(f, path, pattern, out, &yret, (int)(j==0));}end = clock();all += end - start;if(ret > 0)found++;elsenotfound++;}else{printf("can not open file: %s/n", path);}fclose(f);}printf("total time: %d, found: %d, notfound; %d/n", all, found, notfound);return 0; }

總結(jié)

以上是生活随笔為你收集整理的使用flex 做关键词、正则表达式过滤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。