正则表达式练习
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为这看起来像是家庭作业,所以我不会给出直接的答案。您需要研究正则表达式中的修饰符。
*
表示重复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!
^3
2$
1{2,}
^3[1-3]*1{2,}[1-3]*2$
([1-3]*
部分允许任何数字1 到 3 在那里,因为那里没有要求)因此其模式为:
(? (后视和前视以确保 1 被隔离)
^ 3[1-3]*(?
(? (确保在此之前没有 1)
1?$
^((?
希望这会有所帮助。祝你好运,正则表达式很难学,但一旦掌握了它就很容易了。
^3
2$
1{2,}
^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)so the pattern for that is:
(?<!1)(1{2})+(?!1)
(lookbehind and lookahead to ensure 1s are isolated)^3[1-3]*(?<!1)(1{2})+(?!1)[1-3]*2$
(?<!1)12+
(ensure there are no 1s before this one)1?$
^((?<!1)12+)*1?$
Hope this helps. Good luck with regexes, they're hard to learn but easy once you get the hang of it.
我不会为你做作业,但我会指出你正在尝试做的一些问题:
.*
而不是*
来允许任意数量的任意字符。.*(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:
.*
instead of*
to allow any number of any characters..*(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.