Extract a substring using lex regular expressions

Is it possible to extract a substring with lex regular expressions, like it can be done with sed ?

For example

sed -e 's/[0-9]*([a-z]*)[0-9]*/1/'

will extract foo from 34234foo777 or bar from 123bar456

Is it possible to do that with lex like this:

[0-9]*([a-z]*)[0-9]*     printf("alphabetical substring is %sn", /*what?? 1 ??*/);

without parsing yytext by plain C?


There is a solution using Yacc, considering that [0-9] is numeric and [az] is alpha

so your grammar is :

stmt : NUMERIC ALPHA NUMERIC    printf("alphabetical substring is %sn", $2)
     ;

ALPHA and NUMERIC given by the lex

链接地址: http://www.djcxy.com/p/87008.html

上一篇: 使用正则表达式从URL中提取子字符串

下一篇: 使用lex正则表达式提取子字符串