如何使用正则表达式选择任意数量的数字? [英] How to select any number of digits with regex?

查看:59
本文介绍了如何使用正则表达式选择任意数量的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式,它将提取以下 URL 中 directory/ 之后的数字:

I'm trying to write a regex that will extract the numbers after directory/ in the following URL:

http://www.website.com/directory/9892639512/alphanum3r1c/some-more-text-here/0892735235

如果我知道数字的位数,那么我写的这个正则表达式有效:

If I know the number of digits, then this regex I wrote works:

directory\/([0-9]{7})\/

但是,如果我删除匹配{7} 的位数,则正则表达式将停止工作.

However, if I remove the number of digits to match {7}, then the regex stops working.

现场演示:http://regex101.com/r/wX3eI2

我一直在尝试不同的东西,但如果没有明确设置要匹配的字符数,似乎无法让正则表达式工作.

I've been trying different, things, but can't seem to get the regex to work without explicitly setting the number of characters to match.

我怎样才能让它工作?

推荐答案

将正则表达式更改为:

directory\/([0-9]+)\/

{7} 表示 7 个字符(在本例中只有数字).+ 表示一个或多个字符(在本例中为数字).

The {7} means, 7 characters (in this case only numbers). The + means one or more characters (in this case numbers).

这篇关于如何使用正则表达式选择任意数量的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆