正则表达式匹配模式之前得到字符串
问题描述:
我正在一个项目,我必须屏幕刮网站并获得一个字符串。这是文本的一部分。正则表达式匹配模式之前得到字符串
A HREF = “/仪表板/指数/ 2971” 标题= “PROJECT1:PROJECT1”> PROJECT1
我需要得到 “/仪表板/指数/ 2971” 整体的一部分使用正则表达式的文本。目前我有这个:
while(true){
if (buff.readLine()!=null){
String wholeText = buff.readLine();
System.out.println(wholeText.contains("title=Project1"));
htmlCode += buff.readLine() + "\n";
}else{
break;
}
这只是标识“title = Project1”字符串。我需要获取“/ dashboard/index/2971”部分并将其放入一个字符串中。
答
<?php
$str = 'a href = "/dashboard/index/2971" title="Project1:Project1">Projeca...';
preg_match_all('#href\s*=\s*"(.*?)"#', $str, $matches, PREG_SET_ORDER);
$foundURLs = array();
foreach ($matches as $match) {
$foundURLs[] = $match[1];
}
var_dump($foundURLs);