正则表达式 href 匹配数字
好吧,我又回到了正则表达式,我对它的理解很差。花了更多时间学习它,这就是我想到的:
/<a href=\"travis.php?theTaco=([0-9999999])\">(.*)</a>
我基本上想要这个字符串中的数字:
<a href="travis.php?theTaco=510973">510973</a>
我的正则表达式几乎很好?我的原话是:
"/<a href=\"travis.php?theTaco(.*)\">(.*)<\/a>/";
但有时它会给我带来巨大的字符串。所以,我只想获取数字。 我搜索了其他帖子,但有大量不相关的材料,请给出一个示例、资源或指向非常相关问题的链接。
谢谢。
Well, here I am back at regex and my poor understanding of it. Spent more time learning it and this is what I came up with:
/<a href=\"travis.php?theTaco=([0-9999999])\">(.*)</a>
I basically want the number in this string:
<a href="travis.php?theTaco=510973">510973</a>
My regex is almost good? my original was:
"/<a href=\"travis.php?theTaco(.*)\">(.*)<\/a>/";
But sometimes it returned me huge strings. So, I just want to get numbers only.
I searched through other posts but there is such a large amount of unrelated material, please give an example, resource, or a link directing to a very related question.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用您所使用的语言提供的 HTML 解析器。
您的第一个正则表达式失败的原因:
[0-9999999]
不是您想象的那样。与[0-9]
相同,匹配一位数字。要匹配数字,您需要[0-9]+
。另外.*
是贪婪的,会尝试尽可能多地匹配。您可以使用.*?
使其非贪婪。由于您尝试再次匹配数字,因此请再次使用[0-9]+
而不是.*
。此外,如果您要捕获的两个数字相同,您可以只匹配第一个数字,并为第二个数字使用反向引用\1
。还有一些正则表达式元字符需要转义,例如
.
、?
。尝试:
Try using a HTML parser provided by the language you are using.
Reason why your first regex fails:
[0-9999999]
is not what you think. It is same as[0-9]
which matches one digit. To match a number you need[0-9]+
. Also.*
is greedy and will try to match as much as it can. You can use.*?
to make it non-greedy. Since you are trying to match a number again, use[0-9]+
again instead of.*
. Also if the two number you are capturing will be the same, you can just match the first and use a back reference\1
for 2nd one.And there are a few regex meta-characters which you need to escape like
.
,?
.Try:
要捕获数字,您不使用 [0-99999] 这样的范围,而是按数字捕获。像 [0-9]+ 这样的东西更像是你想要的那个部分。另外,正如 codaddict 所说,逃避很重要。
To capture a number, you don't use a range like [0-99999], you capture by digit. Something like [0-9]+ is more like what you want for that section. Also, escaping is important like codaddict said.
其他人已经提到了有关您的正则表达式的一些问题,所以我不会重复它们。
还有关于如何指定您想要的内容的问题。您可以简单地匹配 via
并获取第一个捕获组。您没有向我们提供足够的信息来了解这是否适合您的需求。
Others have already mentioned some issues regarding your regex, so I won't bother repeating them.
There are also issues regarding how you specified what it is you want. You can simply match via
and take the first capturing group. You have not given us enough information to know whether this suits your needs.