简单的正则表达式问题

发布于 2024-11-13 23:30:03 字数 438 浏览 8 评论 0原文

如何匹配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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少女净妖师 2024-11-20 23:30:03

尝试

a{10,}

a 10 次或更多次。

grep -E "a{10,}" filename

匹配 aaaaaaaaaaaaaaaaaaaaaaaaab 但不匹配 aaaaaaaaab

Try

a{10,}

which says a 10 or more times.

grep -E "a{10,}" filename

matches aaaaaaaaaaaaaaaaaaaaaaaaab but not aaaaaaaaab.

一笔一画续写前缘 2024-11-20 23:30:03

如果您的 lexflex,您可以使用a{10,}

如果不是这样,根据
3. Lex 正则表达式
,您可以改用 a{10}a*

If your lex is flex, you can use a{10,}.

If not so, according to
3. Lex Regular Expressions
, you can use a{10}a* instead.

笑梦风尘 2024-11-20 23:30:03

Footy,

[警告:这个答案完全是胡说八道!!!]

(如果你指的是足球,我们就是死敌;-)

嗯,...那就是据我所知,没有使用 sed、grep、nawk 等支持的“标准”正则表达式语法...甚至没有egrep...据我所知, a{ 10,*} 语法(这正是你所渴望的)直到 Perl 重写了所有关于正则表达式功能的书籍才出现......并且(不要引用我的话)我不要认为这种情况直到版本 5 才会发生。

所以,是的,如果您坚持使用 nawk,那么它就是 aaaaaaaaardvarking 硬路家伙。对不起。

干杯。基思.


编辑:

嗯...我似乎是这里的怪人...也许其他人的“标准操作环境”已经更新为稍后识别的“标准工具”正则表达式语法扩展... Sooo... 嗯...我在我的(三岁的)egrepcygwin 实现上测试了这个...它让我感到惊讶通过实际工作!!!

Administrator@snadbox3 ~
$ egrep 'a{3,}b' <<-eof
> ab
> aab
> aaab
> aaaab
> eof
aaab
aaaab

所以我错了一切都结束了......看起来“新”{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!!!

Administrator@snadbox3 ~
$ egrep 'a{3,}b' <<-eof
> ab
> aab
> aaab
> aaaab
> eof
aaab
aaaab

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.

苍白女子 2024-11-20 23:30:03

使用以下格式:a^na*b 并将 n 替换为您想要的任何数字。

use this format : a^na*b and replace n with any number you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文