标签: preg-match-all

如何在Perl中进行全局正则表达式匹配?

我试图在Perl中提出一个匹配多个模式的正则表达式,并像preg_match_allPHP 一样返回所有这些模式.

这就是我所拥有的:

$str = 'testdatastring';
if($str =~ /(test|data|string)/) {
        print "its found index location: $0 $-[0]-$+[0]\n";
        print "its found index location: $1 $-[1]-$+[1]\n";
        print "its found index location: $2 $-[2]-$+[2]\n";
        print "its found index location: $3 $-[3]-$+[3]\n";
}
Run Code Online (Sandbox Code Playgroud)

这只给了我第一场比赛,其中就是'测试'.我希望能够匹配所有出现的指定模式:'test','data'和'string'.

我知道在PHP中,你可以使用preg_match_all来达到这种目的:

if(preg_match_all('/(test|data|string)/', 'testdatastring', $m)) {
        echo var_export($m, true);
}
Run Code Online (Sandbox Code Playgroud)

上面的PHP代码将匹配所有3个字符串:'test','data'和'string'.

我想知道如何在Perl中执行此操作.任何帮助将不胜感激.

php regex perl preg-match-all

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

PHP中的正则表达式:如何在html中为表创建模式

我正在使用最新的PHP.我想解析HTML页面以获取数据.

HTML:

<table class="margin15" style="margin-left: 0pt; margin-right: 0pt;" width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
TRs, TDs, Data
</table>

<table class="margin15" style="margin-left: 0pt; margin-right: 0pt;" width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
TRs, TDs, Data
</table>

<table class="margin15" style="margin-left: 0pt; margin-right: 0pt;" width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
TRs, TDs, Data
</table>

<table class="margin15" style="margin-left: 0pt; margin-right: 0pt;" width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
TRs, TDs, Data
</table>
Run Code Online (Sandbox Code Playgroud)

PHP代码:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.test.com/mypage.html');  
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result …
Run Code Online (Sandbox Code Playgroud)

php preg-match-all

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

preg_match_all确保不同/唯一的结果

我使用以下代码来匹配以'$'开头的脚本中的所有变量,但是我希望结果不包含重复项,即不同/唯一:

preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables);
Run Code Online (Sandbox Code Playgroud)

有什么建议?

php preg-match-all

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

正则表达式锚标记

我正在使用PHP,我有问题从锚标记解析href与文本.

示例:锚标签有测试 http://www.test.com

像这样 <a href="http://www.test.com" title="test">http://www.test.com</a>

我想匹配锚标记中的所有文本

提前致谢.

php regex preg-match-all

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

正则表达式匹配HTML块

首先,我将向您展示我正在使用的代码示例:

<div class="entry">
        <p>Any HTML content could go here!</p>
      </div>
    </div><!--/post -->
Run Code Online (Sandbox Code Playgroud)

通常我会使用如下所示的正则表达式规则来查找前缀和后缀并抓住其间的所有内容:

(?<=<div class="entry">).*(?=</div><!--/post -->)
Run Code Online (Sandbox Code Playgroud)

然而,这看起来似乎没有工作,因为它似乎在以下部分而不是HTML内容本身之间拉动空白区域:

<div class="entry">
        <p>
Run Code Online (Sandbox Code Playgroud)

任何帮助/建议都会非常感激,因为我已经用这个好几个小时抨击我的头了.

提前谢谢了.

php regex preg-match-all

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

php regex preg_match_all无法正常工作

我使用preg_match_all来检查以大写字母开头的日期和单词,问题出现在日期上,因为在正则表达式测试器上它告诉我这个正则表达式没问题但是在php脚本中它没有正确地执行它,我的模式是这样的:

$pattern = "#(((0[1-9]|[12][0-9]|3[01])([\/\.\\\-])((0[1-9]|1[012])\11)?)(\d\d\d\d|\d\d))+|([A-Z][a-z]+)(\s[A-Z][a-z]+)*#";
Run Code Online (Sandbox Code Playgroud)

我想要它匹配:"12.10.1990"以及"12.10.90"

谢谢你的帮助!

php regex preg-match-all preg-match

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

preg_match和图片网址

我对preg_matchPHP中的函数有一些疑问。我认为我永远不会学习如何使用此功能。我想从HTML中提取图像的URL而没有图像名称。例如,如果我有一些图像链接:

"/data/images/2013-10-03/someimage.jpg"
Run Code Online (Sandbox Code Playgroud)

要么

"http://something.com//data/images/2013-10-03/someimage.jpg"
Run Code Online (Sandbox Code Playgroud)

如何使用preg_match函数删除最后一个斜杠的所有剩余内容,以便仅从URL获得图像名称?

也许使用不同的功能会更聪明,但我不知道是哪个功能?

PS:您能给我一些好的preg_match功能教程吗?

也许我忘了说...我不知道图像名称多长时间或确切的图像名称是什么。我需要仅提取最后一个正斜杠右侧的函数。

php preg-match-all preg-match

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

preg_replace更改来自href的链接

我需要替换curl拍摄的页面中的网址,并向图像和链接添加正确的链接。我的PHP curl代码是:

<?php

$result = '<a href="http://host.org"><img src="./sec.png"></a>
<link href="./styles.css" rel="alternate stylesheet" type="text/css" />
<script type="text/javascript" src="./style.js"></script>';

echo $result;
 if (!preg_match('/src="https?:\/\/"/', $result)) {
        $result = preg_replace('/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://google.com/\\3\"", $result);
    }
echo $result;
if (!preg_match('/href="https?:\/\/"/', $result)) {
        $result = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://google.com/\\3\"", $result);
    }
echo $result;

?>
Run Code Online (Sandbox Code Playgroud)

输出为:

//original links
<a href="http://host.org"><img src="./sec.png"></a>
<link href="./styles.css" type="text/css" />
<script src="./style.js"></script><br />

//fixed SRC path
<a href="http://host.org"><img src="http://google.com/./sec.png"></a>
<link href="./styles.css" type="text/css" />
<script src="http://google.com/./style.js"></script>

//fixed HREF path
<a href="http://google.com//google.com/./sec.png"></a>
<link href="http://google.com/./styles.css" type="text/css" />
<script src="http://google.com/./style.js"></script> …
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace preg-match-all preg-match

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

如何只获得PHP中的大写首字母?

我想在缩写中转换短语,但不希望单词的每个第一个字母组成缩写,只需要大写的缩写.例如,我想将字符串"United States of America"转换为"USA"而不是"USOA",因为我发现的所有代码都有效.

在我的项目中,我必须显示类的时间表,并且一些类名称很像"LinguagemdeProgramaçãoOrientadaa Objeto"(面向对象的编程语言),如果我使用我在互联网上找到的代码,它将"LinguagemdeProgramaçãoOrientadaa Objeto"字符串转换为LDPOAO,而不是LPOO,因为它是有意义的.

我找到的代码:(它将一个字符串变成一个缩写,我想知道如何只选择输入$ result的大写字母)

$string = "Progress in Veterinary Science";

$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, $string, $matches);

$result = implode('', $matches[0]);

$result = strtoupper($result);

echo $result;
Run Code Online (Sandbox Code Playgroud)

php regex abbreviation preg-match-all

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

如何在php中的文本内找到自定义标签

我在寻找PHP解决方案,我有一些带有一些自定义标记的HTML内容,例如

$html = "he approaches very silently towards him but at the last point of time, the man gets the hint and manages to escape from the attack. ,
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid] What happens in the latter is just breath-taking and comes with a reason why this video has gone viral in such short span of time";
Run Code Online (Sandbox Code Playgroud)

我只想要下面提到的文本的输出:

[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid]

   or
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [Endvid]
Run Code Online (Sandbox Code Playgroud)

$ FWithReplaceWord将是youtube“ v =” ID eED6VRcj1Rs

需要输出

<div class='embed-responsive embed-responsive-16by9 vid1'><iframe class='embed-responsive-item' src='//www.youtube.com/embed/$FWithReplaceWord' allowfullscreen></iframe></div>
Run Code Online (Sandbox Code Playgroud)

我在用 …

php preg-match-all

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

标签 统计

php ×10

preg-match-all ×10

regex ×6

preg-match ×3

abbreviation ×1

perl ×1

preg-replace ×1