PHP正则表达式在html之间匹配数据
我创建了一个正则表达式,它实际上提取了我需要的数据,但它还包括“>”性格,我该如何摆脱它?这是代码。
<?php
$content = file_get_contents('www.example.com');
$pattern = "/>([0-9]{2}\.[0-9]{3})/";
preg_match_all($pattern, $content, $matches);
echo $matches[0][2];
?>
从中提取的 HTML
<td style="text-align:right" class="row">23.020</td>
给出了“<23.020”,但我需要的是“23.020” 我知道这是一个n00b问题,但我如何摆脱“<”
i have created a regex, that actually extracts the data what i need, but it also includes ">" character, how do i get rid of it? Here's the code.
<?php
$content = file_get_contents('www.example.com');
$pattern = "/>([0-9]{2}\.[0-9]{3})/";
preg_match_all($pattern, $content, $matches);
echo $matches[0][2];
?>
and the HTML to extract from
<td style="text-align:right" class="row">23.020</td>
it gives me the "<23.020" but what i need is "23.020"
i know it's a n00b question, but how do i get rid of the "<"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
会给你
所以只需使用
$matches[1][0]
。will give you
So simply use
$matches[1][0]
.如果您想匹配正则表达式中的某些内容,但不捕获它,那么您可以使用“断言< /a>”。对于您的字符串,它将是
(?<=[>])
后视。但是,在您的情况下,您已经有一个不包括
>
锚点的捕获组。您只需要访问正确的结果组即可:[1]
引用内部(...)
括号组,而您的[0]< /code> 将返回完整的匹配。
If you want to match something in a regex, but not capture it, then you can use an "assertion". For your string it would be a
(?<=[>])
lookbehind.In your case however, you already have a capture group which excludes the
>
anchor. You just need to access the right result group then:The
[1]
refers to the inner(...)
parens group, whereas your[0]
would return the complete match.