正则表达式在匹配模式之前获取字符串

时间:2012-02-21 20:25:16

标签: java regex

我正在开展一个项目,我必须筛选网站并获取字符串。这是文本的一部分。

  

a href =“/ dashboard / index / 2971”   标题= “PROJECT1:PROJECT1” > PROJECT1

我需要使用正则表达式获取整个文本的“/ dashboard / index / 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”部分并将其放在一个字符串中。

1 个答案:

答案 0 :(得分:0)

<?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);