使用正则表达式捕获不确定数量的数字?
我想在可能包含无限数量数字的字符串上使用正则表达式来捕获。我的直觉引导我执行 "/\.getnumbers (\d+)+\s*/"
但这只匹配 .getnumbers
命令后面的第一个数字。如何编写一个正则表达式语句,该语句将捕获命令后由简单空格分隔的一个或多个数字。例如:.getnumbers 5 4 3 2 1
将匹配 (5) (4) (3) (2) (1)
,尽管正则表达式没有专门编写匹配5个数字,它可以匹配任意数量的数字。
I want to get captures using regex on a string that could contain an indefinite amount of numbers. My intuition lead me to do "/\.getnumbers (\d+)+\s*/"
but that only matched the first number following the .getnumbers
command. How do I write a regex statement that will capture one or more numbers after the command separated by a simple space. For example: .getnumbers 5 4 3 2 1
would match (5) (4) (3) (2) (1)
, though the regex isn't specifically written to match 5 numbers, it could match any amount of numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有后处理,您可能无法做到这一点,因为大多数正则表达式引擎不允许无限数量的组。幸运的是,后处理仅包括按空格分割。
You probably can't do it without postprocessing, since most regex engines don't allow an indefinite number of groups. Fortunately the postprocessing consists only of splitting by spaces.
请注意,您将获得作为单个捕获组的所有数字。例如:“5 4 3 2 1”
Note that you'll get all of the numbers as a single capture group. eg: "5 4 3 2 1"