Ruby Regex for String,其中包含X个整数数量
我想制作一个与包含X整数数字的字符串相匹配的Ruby Regex。我希望字符串也能够包含其他单词。
例子: 如果 x = 2 ,则应匹配: “每小时有3辆车超过45英里”
第一个整数为3,第二个整数为45。
I want to craft a ruby regex that matches strings that contain exactly x integer numbers. I want the string to also be able to contain other words as well.
Example:
If x = 2, the following should match:
"There were 3 cars going over 45 miles per hour"
where the first integer is 3 and the second integer is 45.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会写的
或
后者具有优势,即
str.gsub(/\ d+)
返回枚举器,而str.Str.scan(/\ d+/)
创建一个临时数组。请参阅 string#gsub 进行论点,但没有块, enum of noreferrer“> /a>。
I would write
or
The latter has the advantage that
str.gsub(/\d+)
returns an enumerator, whereasstr.scan(/\d+/)
creates a temporary array.See the form of String#gsub that takes an argument but no block, and Enumerable#count.
您可以使用一般的正则方式:
您可以将
{2}
替换为字符串中的许多数字。这是一个工作 demo 。You may use the general regex pattern:
You may replace the
{2}
with however many numbers you expect in the string. Here is a working demo.