简单的正则表达式问题
如何匹配aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab,其中a的数量应至少为10?
我的意思是我知道这样:
[a][a][a][a][a][a][a][a][a][a][a][a][a]a*b
但是如果我的a的最小数量变成100,那么一定有一个更好的优雅方法。
它是什么?我正在尝试匹配 (a^n)b 之类的东西,其中 n 可以是任何东西
编辑:
我忘了提到这是使用 lex 和 yacc 完成的..其中 lex 必须返回一个令牌yacc。
%{
#include "y.tab.h"
%}
%%
aaaaaaaaaa[a]*b {return ok;}
\n {return '\n';}
. {return 0;}
%%
How to match aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab where number of a's should be min of 10?
I mean i know this way:
[a][a][a][a][a][a][a][a][a][a][a][a][a]a*b
But there must be a better elegant method where is if my min number of a's become say 100..
What is it? I am trying to match (a^n)b sort of thing where n can be anything
EDIT:
I forgot to mention this is done using lex and yacc.. where the lex has to return a token to yacc.
%{
#include "y.tab.h"
%}
%%
aaaaaaaaaa[a]*b {return ok;}
\n {return '\n';}
. {return 0;}
%%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
说
a
10 次或更多次。匹配
aaaaaaaaaaaaaaaaaaaaaaaaab
但不匹配aaaaaaaaab
。Try
which says
a
10 or more times.matches
aaaaaaaaaaaaaaaaaaaaaaaaab
but notaaaaaaaaab
.如果您的
lex
是 flex,您可以使用a{10,}
。如果不是这样,根据
3. Lex 正则表达式
,您可以改用
a{10}a*
。If your
lex
is flex, you can usea{10,}
.If not so, according to
3. Lex Regular Expressions
, you can use
a{10}a*
instead.Footy,
[警告:这个答案完全是胡说八道!!!]
(如果你指的是足球,我们就是死敌;-)
嗯,不...那就是据我所知,没有使用 sed、grep、nawk 等支持的“标准”正则表达式语法...甚至没有egrep...据我所知,
a{ 10,*}
语法(这正是你所渴望的)直到 Perl 重写了所有关于正则表达式功能的书籍才出现......并且(不要引用我的话)我不要认为这种情况直到版本 5 才会发生。所以,是的,如果您坚持使用 nawk,那么它就是 aaaaaaaaardvarking 硬路家伙。对不起。
干杯。基思.
编辑:
嗯...我似乎是这里的怪人...也许其他人的“标准操作环境”已经更新为稍后识别的“标准工具”正则表达式语法扩展... Sooo... 嗯...我在我的(三岁的)egrep 的 cygwin 实现上测试了这个...它让我感到惊讶通过实际工作!!!
所以我错了一切都结束了......看起来“新”
{min,[max]}
语法得到了相当好的支持,而我已经老了。叹。干杯。基思.
Footy,
[WARNING: This answer is COMPLETE BUNKUM!!!]
(if you mean soccer, we're swarn enemies ;-)
Ummm, No... That is not as far as I know, using "the standard" regular expression syntax as supported by sed, grep, nawk, and the likes... and no not even egrep... As far as I know, the
a{10,*}
syntax (which is exactly what you're hankering for) didn't emerge until Perl rewrote all the books on the capabilities of regular expressions... and (don't quote me on this) I don't think that happened until like version 5.So yeah, If you're stuck with using nawk, then it's the aaaaaaaaardvarking hardway dude. Sorry.
Cheers. Keith.
EDIT:
Hmmm... I seem to be the odd-man-out here... maybe everone-elses "standard operating environment(s)" have been updated with "standard tools" that recognise later regular expression syntax extensions... Sooo... Hmmm... I tested this on my (three year old) cygwin implementation of egrep... and it suprised me by actually working!!!
So I'm WRONG all ends up... looks like the "new"
{min,[max]}
syntax is reasonably well supported, and I'm getting old. Sigh.Cheers. Keith.
使用以下格式:
a^na*b
并将 n 替换为您想要的任何数字。use this format :
a^na*b
and replace n with any number you want.