正则表达式练习

发布于 2024-12-12 18:22:59 字数 793 浏览 1 评论 0原文

L= { w is {1,2,3}* 
   | w starts with 3, ends with 2 and there is a substring of only 1 with length 
       even equal or >2}.

所以一些测试的结果必须是:

3323112: accepted
311211112: non accepted
31112: non accepted
32: non accepted
2113: non accepted
313212: non accepted

我的答案是: 3*(11)*2*

但它失败了一些测试......有人可以帮助我吗?

第二个练习是:

L= { w is {1,2}* 
   | in w after every 1 there is one or more 2, but if the 1 is the last 
     character it could be the last (no 2 after it)}

测试字符串:

1: accepted
222:  accepted
221212122:  accepted
1222121:  accepted
111221: not  accepted
11: not  accepted

我的解决方案是 (12*)*

但它失败了一些测试......请帮助我。

L= { w is {1,2,3}* 
   | w starts with 3, ends with 2 and there is a substring of only 1 with length 
       even equal or >2}.

So result of some tests must be:

3323112: accepted
311211112: non accepted
31112: non accepted
32: non accepted
2113: non accepted
313212: non accepted

My answer is : 3*(11)*2*

But it fails some tests... Can someone help me?

The 2nd exercise is :

L= { w is {1,2}* 
   | in w after every 1 there is one or more 2, but if the 1 is the last 
     character it could be the last (no 2 after it)}

Test strings:

1: accepted
222:  accepted
221212122:  accepted
1222121:  accepted
111221: not  accepted
11: not  accepted

My solution is (12*)*

But it fails some tests... Help me please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

傲影 2024-12-19 18:22:59

因为这看起来像是家庭作业,所以我不会给出直接的答案。您需要研究正则表达式中的修饰符。 *表示重复0次或多次。还有用于字符类的 +? 和方括号。另请注意,某些可用的内容可能取决于您使用的正则表达式解析器(通常称为“风味”)。但一些基础知识通常是相同的。

祝你好运!

Since this seems like homework I'm not gonna give a straight answer. You'll want to look into the modifiers in regular expressions. * denotes a repetition of 0 or more times. There's also +, ? and square brackets for character classes. Also note that some stuff that's available may be dependent on the regex parser (often called "flavour") you're using. But some basics are normally the same.

Good luck!

世界和平 2024-12-19 18:22:59
  • 以 3 开头:^3
  • 以 2 结尾:2$
  • 包含至少两个 1 的序列:1{2,}
  • 结果正则表达式:^3[1-3]*1{2,}[1-3]*2$[1-3]* 部分允许任何数字1 到 3 在那里,因为那里没有要求)
  • 编辑:我想我误解了关于 1 的条件,
    因此其模式为: (? (后视和前视以确保 1 被隔离)
  • 修改后的模式: ^ 3[1-3]*(?

  • 每一个 1 后面至少有一个 2: (? (确保在此之前没有 1)
  • 最后一个 1 之后可能没有 2: 1?$
  • 结果:^((?

希望这会有所帮助。祝你好运,正则表达式很难学,但一旦掌握了它就很容易了。

  • Starts with a 3: ^3
  • Ends with a 2: 2$
  • Contains a sequence of at least two 1s: 1{2,}
  • Resulting regex: ^3[1-3]*1{2,}[1-3]*2$ (the [1-3]* pieces allow for any digits 1 to 3 in there, since there are no requirements there)
  • EDIT: I think I misunderstood the condition about the 1s,
    so the pattern for that is: (?<!1)(1{2})+(?!1) (lookbehind and lookahead to ensure 1s are isolated)
  • Revised pattern: ^3[1-3]*(?<!1)(1{2})+(?!1)[1-3]*2$

  • After every 1 there is at least one 2: (?<!1)12+ (ensure there are no 1s before this one)
  • The final 1 might not have a 2 after: 1?$
  • Result: ^((?<!1)12+)*1?$

Hope this helps. Good luck with regexes, they're hard to learn but easy once you get the hang of it.

小鸟爱天空丶 2024-12-19 18:22:59

我不会为你做作业,但我会指出你正在尝试做的一些问题:

  • 星号本身并不是通配符。它并不意味着“匹配任何东西”。它是一个量词修改前一个标记。您希望使用 .* 而不是 * 来允许任意数量的任意字符。
  • 表达式 .*(11)*.* 也匹配具有奇数个 1 的字符串,因为 . 也可以是 1。 1 之前和之后的数字必须是“not-a-1”(即 2 或 3)。

希望这对您有所帮助。

I'm not going to do your homework for you but I'll point out a few problems with what you are trying to do:

  • A star on its own is not a wildcard. It does not mean "match anything". It is a quantifier that modifies the previous token. You want to use .* instead of * to allow any number of any characters.
  • The expression .*(11)*.* also matches strings with an odd number of 1s, because the . can also be one. The digit immediately before and after the 1s must be "not-a-1" (i.e. 2 or 3).

Hopefully this should help you.

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