Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带“的正则表达式&;nbsp";专注于内容_Php_Regex - Fatal编程技术网

Php 带“的正则表达式&;nbsp";专注于内容

Php 带“的正则表达式&;nbsp";专注于内容,php,regex,Php,Regex,在尝试捕获此HTML中的某些数据时,正则表达式遇到问题: <ul> <li>Nombre de mots à traduire&nbsp;:&nbsp;41 mots.</li> <li>Nombre de mots partiellement traduits&nbsp;:&nbsp;164 mots.</li> <li>Nombre de mots

在尝试捕获此HTML中的某些数据时,正则表达式遇到问题:

<ul>
       <li>Nombre de mots à traduire&nbsp;:&nbsp;41 mots.</li>
       <li>Nombre de mots partiellement traduits&nbsp;:&nbsp;164 mots.</li>
       <li>Nombre de mots traduits&nbsp;:&nbsp;792 mots.</li>
       <li>Nombre de correspondances exactes&nbsp;:&nbsp;808 mots.</li>
       <li>Nombre de répétitions internes&nbsp;:&nbsp;71 mots.</li>
       <li>Total&nbsp;:&nbsp;1876 mots.</li>
</ul>

我想得到“FRA(FRA)”和“ESP(ESP)”,你知道吗?

如果你需要每个
  • MOT的数量,你可能应该使用这样的正则表达式:

    (\d+)\smots
    
    Langue.*([A-Z]{3})\s\(\1\)
    
    但是请注意,如果您试图解析HTML,最好使用HTML解析器,因为正则表达式很难处理非正则语法(即HTML、XML)

    更新

    对于您的第二个查询,我将尝试以下内容:

    (\d+)\smots
    
    Langue.*([A-Z]{3})\s\(\1\)
    
    在上面的例子中,第一个捕获组应该是语言。正则表达式最后部分中的
    \1
    表示第一个捕获组,这意味着
    FRA(FRA)
    将匹配,但
    FRA(BLA)
    将不匹配

    您可以使用:

    preg_match_all('~[0-9]+(?= mots.</li>)~', $html, $matches);
    print_r($matches);
    
    preg_match_all(“~[0-9]+(?=mots.
  • )~”,$html,$matches); 打印(匹配项);
    或者更明确地说:

    preg_match_all('~(?J)<li>(?:Nombre de (?<what>[^&]++)|(?<what>Total))[^0-9]+(?<quantity>[0-9]+)[^<]*</li>~i', $html, $matches, PREG_SET_ORDER);
    print_r($matches); 
    

    preg_match_all('~(?J)
  • (?:Nombre de(?[^&]+)|(?Total))[^0-9]+(?[0-9]+)[^使用DOM解析器,你就不必担心这个问题了。这很有效,谢谢!我编辑了这个问题,你知道怎么做吗?使用HTML解析器有什么好处?这很有效,谢谢!这是使用mots的好方法。我编辑了这个问题,你知道怎么做吗?