匹配特定位置之间没有特定图案的字符串

时间:2014-12-05 01:06:28

标签: php regex

$example_string = "<a class="190"><br>hello.. 8/10<br><a class="154"><br>9/10<br>"

我需要匹配的是类和“评级”部分(8/10)。

像这样的东西,除了我不知道如何在regexp中写(ANYTHING EXCEPT <br> here)

preg_match_all('#class="([0-9]{3})"><br>(ANYTHING EXCEPT <br> here)*?([0-9]/10)#', 
$example_string, matches);

所以preg_match_all应该给出这些结果:

$matches[1][1] = '190';
$matches[1][2] = '8/10';
$matches[2][1] = '154';
$matches[2][2] = '9/10';

4 个答案:

答案 0 :(得分:1)

解决你的模式,并回答你的问题

class="([0-9]{3})"><br>(?:(?!<br>).)*?([0-9]\/10)

Demo

答案 1 :(得分:0)

我不知道php,但它应该像在python中一样工作......

获取“类”之间的匹配,并迭代以在返回的匹配字符串中获取数据

import re # the regex module
example_string = '"<a class="190"><br>hello.. 8/10<br><a class="154"><br>9/10<br>"'

for match in re.findall(r'(?:class[^\d]")([^\/]+)(?!class)', example_string):
    print(list(re.findall(r'(\d+)', match)))

产生以下列表:

['190', '8']
['154', '9']

答案 2 :(得分:0)

一个简单的DOM解析器可以为您提供这些信息:

$example_string = '<a class="190"><br>hello.. 8/10<br><a class="154"><br>9/10<br>';

$dom = new DOMDocument;
$dom->loadHTML($example_string);
$xpath = new DOMXPath($dom);

// get all text nodes that have an anchor parent with a class attribute
$query = '//text()[parent::a[@class]]';

foreach ($xpath->query($query) as $node) {
    echo $node->textContent, "\n";
    echo "parent node: ", $node->parentNode->getAttribute('class'), "\n";
}

输出

hello.. 8/10
parent node: 190
9/10
parent node: 154

答案 3 :(得分:0)

(?<=class=")(\d+)|(\d+\/\d+)

试试这个。看看演示。

https://regex101.com/r/yR3mM3/58

$re = "/(?<=class=\")(\\d+)|(\\d+\\/\\d+)/";
$str = "<a class=\"190\"><br>hello.. 8/10<br><a class=\"154\"><br>9/10<br>";

preg_match_all($re, $str, $matches);