标签: preg-match-all

为什么我在RegExp中得到空结果?

我正在尝试使用此RegExp将我的URL解析为不同的部分:

([\w\\.-]*)
Run Code Online (Sandbox Code Playgroud)

给出一个示例URL http://www.foo.com/bar/baz我从preg_match_all()获得这些结果:

Array
(
[0] => Array
    (
        [0] => http
        [1] => 
        [2] => 
        [3] => 
        [4] => www.foo.com
        [5] => 
        [6] => bar
        [7] => 
        [8] => baz
        [9] => 
    )

)
Run Code Online (Sandbox Code Playgroud)

它似乎将任何无效字符解析为空项.
我该如何解决这个问题?

php regex preg-match-all

1
推荐指数
1
解决办法
95
查看次数

得到所有出现的字符串并替换它们

示例文本

this text will be left alone ##{class1}{arg1;arg2;arg3}## some other text and
another thingie ##{Class2}{]##
Run Code Online (Sandbox Code Playgroud)

目前我正在使用它'/\#\#\{(?P<class>.+)\}\{(?P<args>.+)\}\#\#/i'来匹配它,但我的问题是当这个正则表达式多次出现时,因为它甚至会匹配两个匹配模式之间的文本.

这是我的正则表达式的结果##{ClassName1}{arg1;arg2;arg3}## some other random ##{class2}{}##.

是否有更优雅的方法从字符串中提取##{class1}{arg1;arg2;arg3}####{Class2}{]##用相应类的输出替换它们?

我正在使用它来替换Class1和Class2的输出,其中参数介于第二组之间 {}


注意:如果第二组{}为空,则表示没有参数传递给第一组{}中具有名称的类

php regex preg-match-all

1
推荐指数
1
解决办法
63
查看次数

PHP RegEx 不会出现问号

我正在尝试使用 PHP 函数“preg_match_all”来匹配字符串中的 url。它工作正常,但它不会匹配带有问号的网址。

例如,这将很好地匹配:

http://espn.com/mlb
Run Code Online (Sandbox Code Playgroud)

但这不会匹配:

http://espn.com/mlb?player=71
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的正则表达式,

$regexUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
Run Code Online (Sandbox Code Playgroud)

我不明白为什么问号没有被 \S 选中。我尝试了很多不同的表达方式,但无法让问号匹配。有任何想法吗?

编辑:

事实证明 preg_match_all 返回 true,但我没有转义 preg_match_all 输出中的问号,因此我稍后进行的 preg_replace 调用失败了。

php regex preg-match-all

1
推荐指数
1
解决办法
3958
查看次数

php preg_match_all 括号中的数字

我正在尝试加载一个远程网站并获取括号内的所有数字。但最终发生的是它只匹配最后一个值。

我的正则表达式错了吗?我是否使用了正确的标志?

我在第二个 $html 变量中添加了它应该匹配的示例。

    //$html = file_get_contents("http://example.com/test.html");

    $html = "(1234) (12) (1)  \r\n  (1346326)";
    preg_match_all("^[(\d)]+$^", $html, $matches, PREG_PATTERN_ORDER);
    print_r($matches);
    echo "<br>";
    foreach ($matches as $val) {
        echo "matched: " . $val[0] . "\n";

    }
Run Code Online (Sandbox Code Playgroud)

谢谢。

php regex numeric preg-match-all

1
推荐指数
1
解决办法
2270
查看次数

使用preg_match_all从字符串中提取img src

我一直试图使用preg_match_all30分钟,但看起来我做不到.

基本上我有一个$var包含HTML代码的字符串.例如:

<br>iihfuhuf
<img title="Image: http://www.jlnv2.local/temp/temp513caca536fcd.jpeg"   
 src="http://www.jlnv2.local/temp/temp513caca536fcd.jpeg">
<img src="http://www.jlnv2.local/temp/temp513caca73b8da.jpeg"><br>
Run Code Online (Sandbox Code Playgroud)

我想获取包含在其值中srcimg标记的属性值./temp/temp[a-z0-9]{13}\.jpegsrc

这是我到目前为止:

preg_match_all('!(<img.*src=".*/temp/temp[a-z0-9]{13}\.jpeg"(.*alt=".*")?>)!', $content, $matches);
Run Code Online (Sandbox Code Playgroud)

php regex html-parsing preg-match-all

1
推荐指数
1
解决办法
2万
查看次数

Preg匹配都不会找到字符串

我的preg_match_all功能:

preg_match_all("{lang:(.*?)}", $template, $found_langs);
Run Code Online (Sandbox Code Playgroud)

模板如下:

<h1>{lang:Choose sport}</h1>
Run Code Online (Sandbox Code Playgroud)

但它不会找到它...但是如果我使用它:

preg_match_all("{lang:(\w*)}", $template, $found_langs);
Run Code Online (Sandbox Code Playgroud)

它会找到Choose.我需要找到Choose sport..

谁知道为什么(.*?)不起作用?

php regex preg-match-all

1
推荐指数
1
解决办法
92
查看次数

无法匹配某种字符串格式

我试图写一个preg_match匹配给定的字符串,我无法正确.

我想要匹配的是这部分在长文本中:

Adrian asdzxc

我尝试的规则看起来像这样,但它失败了:

preg_match('#<div class="instant_search_title fsl fwb fcb"><a href="(?:[^"]+)">([^<]+)</a>#i', $file_contents, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)

长文是这样的:

<i class="mrs friendsIcon customimg img sp_b6fmvb sx_5e511f"><u>Friend</u></i><span class="uiButtonText">Friends</span></a></div></div><div class="pls"><div class="instant_search_title fsl fwb fcb"><a href="https://www.facebook.com/adrianasdzxc" onclick="if (event.button == 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:name&quot;&#125;);  &#125;" onmouseup="if (event.button != 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:name&quot;&#125;);  &#125;" data-hovercard="/ajax/hovercard/user.php?id=616226XXX">Adrian asdzxc</a></div><div class="fsm fwn fcg"><div class="fbProfileByline searchResultPersonByline"><span class="fbProfileBylineFragment"><span class="fbProfileBylineIconContainer"><i class="mrs fbProfileBylineIcon img sp_9pvis2 sx_897cc1"></i></span><span class="fbProfileBylineLabel">Webmaster at <a href="true"><a href="https://www.facebook.com/pages/Freelancer/640564905962853" data-hovercard="/ajax/hovercard/page.php?id=640564905962853">Freelancer</a></a></span></span><span class="fbProfileBylineFragment"><span class="fbProfileBylineIconContainer"><i class="mrs fbProfileBylineIcon img sp_9pvis2 sx_ed189a"></i></span><span class="fbProfileBylineLabel">Studied Web Design at <a href="true"><a href="https://www.facebook.com/pages/Universitatea-Tibiscus/242668875759811" data-hovercard="/ajax/hovercard/page.php?id=242668875759811">Universitatea …
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all preg-match

1
推荐指数
1
解决办法
186
查看次数

PHP 使用 preg_match_all 获取所有链接和图像

从新闻网站我们有很多标签,例如:

<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">
   <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">
</a>
Run Code Online (Sandbox Code Playgroud)

我想从中获取href图像src和属性值。alt

我的简单代码不正确:

<meta charset='UTF-8' />
<?php
error_reporting(1);
$handle='http://sample.com';
$handle = file_get_contents($handle);
preg_match_all('/<a[^>]+class="titr1y"[^>] href="(.*?)"*><img class="[^>]" width="[^>]" style="[^>]" src="(.*?)" alt="(.*?)"*><\/a>/si', $handle, $matching_data);

print_r($matching_data);
?>
Run Code Online (Sandbox Code Playgroud)

请帮我改正。

php preg-match-all

1
推荐指数
1
解决办法
2508
查看次数

如何使用正则表达式匹配400到550之间的数字?

我想验证一个数字字符串,该字符串必须只包含400550之间的数字.

$x = "401";  // valid 
$x = "551";  // invalid 
Run Code Online (Sandbox Code Playgroud)

我想只匹配400550之间的数字.

我尝试了以下模式:

if(preg_match_all("/[400-550]+/",$x)); 
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

匹配数字400-550的正则表达式是什么?

php regex preg-match-all

1
推荐指数
2
解决办法
901
查看次数

PHP正则表达式找到双括号

我提交了一些文本,它将在双括号中包含单词,但是在单括号中只能包含一些单词。但是我只想要放在方括号中的那些。

例:

[[测试]] [测试1] [[测试2]] [测试5] [[测试3]]

与其他字词等在同一字符串中...

因此,例如:

[[test]]快速[test1]棕色[[test2]]狐狸[[test3]]在懒惰的[[test4]]狗上跳了[mytest]

现在我有一个preg_match_all与:

(preg_match_all('/\\[.*?\\]\\]/is',$str,$match))
Run Code Online (Sandbox Code Playgroud)

但是,它也拉了单括号的...我正则表达式很烂。

有人对我如何只拉双括号有任何意见吗?

谢谢!

php regex preg-match-all

1
推荐指数
1
解决办法
808
查看次数

标签 统计

php ×10

preg-match-all ×10

regex ×9

html-parsing ×1

numeric ×1

preg-match ×1