正则练习:阶乘
这是 StackOverlow 的一个实验性新功能:通过解决各种经典问题来锻炼你的正则表达式能力。没有唯一正确的答案,事实上我们应该收集尽可能多的正确答案,只要它们具有教育价值。接受所有口味,但请清楚记录。尽可能提供测试用例/片段来证明该模式“有效”。
我们如何使用正则表达式查找数字x是否是阶乘?
额外奖励:如果模式可以确定x = n!,它也可以找到n吗?
This is an experimental new feature for StackOverlow: exercising your regex muscles by solving various classical problems. There is no one right answer, and in fact we should collect as many right answers as possible, as long as they offer educational value. All flavors accepted, but please document it clearly. As much as practical, provide testcases/snippets to demonstrate that the pattern "works".
How can we find if a number x is a factorial using regex?
Bonus: if the pattern can determine that x = n!, can it also find n?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java,具有无限长度的后向查找和嵌套引用(另请参阅 ideone.com):
Java, with infinite length lookbehind and nested references (see also on ideone.com):
使用 C# 中的 .NET 平衡组(另请参阅 ideone.com):
注意:
.NET 的版本ideone.com 使用的平衡组似乎有一个错误,导致上面的代码片段中不得不重复
+?
。在较新的版本中,简单的贪婪+
可能就足够了。另请参阅:回溯平衡组贪婪重复可能会导致不平衡?With .NET balancing groups, in C# (see also on ideone.com):
Note:
The version of .NET used by ideone.com seems to have a bug in the balancing groups that made the reluctant repetition
+?
necessary in the above snippet. In newer versions, a simple greedy+
may suffice. See also: Backtracking a balancing group in a greedy repetition may cause imbalance?