防爆帮助:Found: 84 Displaying: 1 - 84
PHP:请使用的preg_match
我想preg_match
走出数84
Found
和Displaying
之间,但我在正则表达式非常糟糕。
你知道什么好的教程来学习正则表达式吗?我在Google上找不到一个好的。
编辑从下面的评论插入:
我这里只是简化了我的问题。真正的问题,我会发现它在一个完整的HTML页面,如谷歌搜索。你知道我的意思吗?
防爆帮助:Found: 84 Displaying: 1 - 84
PHP:请使用的preg_match
我想preg_match
走出数84
Found
和Displaying
之间,但我在正则表达式非常糟糕。
你知道什么好的教程来学习正则表达式吗?我在Google上找不到一个好的。
编辑从下面的评论插入:
我这里只是简化了我的问题。真正的问题,我会发现它在一个完整的HTML页面,如谷歌搜索。你知道我的意思吗?
如果您的输入始终采用相同的格式,则无需使用正则表达式。相反,只是在分割空间的字符串:
// explode() on spaces, returning at most 2 array elements.
$parts = explode(" ", "Found: 84 Displaying: 1 - 84", 2);
echo $parts[1];
更新如果你真的真的真的想用preg_match()
这一点,这里的如何。这不是建议这样简单的应用程序。
// Array will hold matched results
$matches = array();
$input = "Found: 84 Displaying: 1 - 84";
// Your regex will match the pattern ([0-9]+) (one or more digits, between Found and Displaying
$result = preg_match("/^Found: ([0-9]+) Displaying/", $input, $matches);
// See what's inside your $matches array
print_r($matches);
// The number you want should be in $matches[1], the first subgroup captured
echo $matches[1];
偏题:我觉得很多“正则表达式”的问题可以通过分割和找到结果来充分回答。 –
好主意,但我仍然想知道如何使用preg_match :-)谢谢。 – Quy
@JaredFarrish如果每次我回答'explode()'到一个正则表达式问题时我都有一美元......(我每个人都有10-20个代表,但它不一样:)) –
相当简单的正则表达式,我包括使用它的PHP代码:
<?php
preg_match("/(\d+)/", "Found: 84 Displaying: 1 - 84", $matches);
//$matches[0] should have the first number, i.e. 84
echo $matches[0]; // outputs "84"
?>
http://www.regular-expressions.info/有关于如何写正则表达式一些很好的信息。
编辑:如前所述,在这种情况下正则表达式是矫枉过正的,标记化工作正常。
效果很好。谢谢 – Quy
你一定找到http://www.regular-expressions.info/ –
_现在你有两个问题... _ http://www.codinghorror.com/blog/2008/06/regular-expressions-now -you-have-two-problems.html :) –