从正则表达式获得多结果

时间:2015-06-12 11:59:29

标签: php regex

我想从Regex获得多个结果,但它没有正确工作。

测试字符串:

<div class="row-fluid pybar-row">
    <span class="pybar-label text-left">
        <span class="">Male</span>
    </span>
    <span class="pybar-bars">
    <div class="row-fluid">
        <span class="pybar-bar pybar-l" style="">
            <span class="pybar-bg">
                <span style="width:100%">&nbsp;</span>
            </span>
        </span>
        <span class="pybar-bar pybar-r">
            <span class="pybar-bg">
                <span style="width:78%;">&nbsp;</span>
            </span>
        </span>
    </div>
</div>

我想只得到一些风格=&#34;宽度: X %&#34;

我使用了这个正则表达式:

$re = "/<span class=\"pybar-bg\">.*?width:(.*?)%/s";

但它只返回第一个数字。我希望字符串中的所有数字都有这个类。

2 个答案:

答案 0 :(得分:4)

你的正则表达式是正确的。使用$s = <<<HTML <div class="row-fluid pybar-row"> <span class="pybar-label text-left"> <span class="">Male</span> </span> <span class="pybar-bars"> <div class="row-fluid"> <span class="pybar-bar pybar-l" style=""> <span class="pybar-bg"> <span style="width:100%">&nbsp;</span> </span> </span> <span class="pybar-bar pybar-r"> <span class="pybar-bg"> <span style="width:78%;">&nbsp;</span> </span> </span> </div> </div> HTML; $re = "/<span class=\"pybar-bg\">.*?width:(.*?)%/s"; if( preg_match_all( $re, $s, $matches ) ) { var_dump( $matches[1] ); } 提取所有匹配项:

array(2) {
  [0] =>
  string(3) "100"
  [1] =>
  string(2) "78"
}

这将输出:

import UIKit

class FirstTableViewController: UITableViewController{


var FirstTableArray = [String]()

var SecondArray = [SecondTable]()

var ThirdArray = [ThirdView]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FirstTableArray = ["First", "Second", "Third","Fourth"]


    SecondArray =
        [SecondTable(SecondTitle: ["jdhfhjd","kldjjlkdfjd"]),
            SecondTable(SecondTitle: ["FirstSecond","SecondSecond","ThirdSecond"]),
    SecondTable(SecondTitle: ["FirstThird","SecondThird","ThirdThird"]),
    SecondTable(SecondTitle: ["FirstFourth"])]

    ThirdArray = [ThirdView(ThirdViewArray: [lf1,lf2]),
    ThirdView(ThirdViewArray: ["asdkljf","asdfasd","asdfas"]),
    ThirdView(ThirdViewArray: ["asdkljf","asdfasd","asdfas"]),
    ThirdView(ThirdViewArray: ["asdkljf"])]


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return FirstTableArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell


    Cell.textLabel?.text = FirstTableArray[indexPath.row]

    return Cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!

    var DestViewController = segue.destinationViewController as! SecondTableViewController

    var SecondTableArrayTwo : SecondTable

    SecondTableArrayTwo = SecondArray[indexPath.row]

    DestViewController.SecondArray = SecondTableArrayTwo.SecondTitle


    var ThirdAnswerArray : ThirdView

    ThirdAnswerArray = ThirdArray[indexPath.row]

    DestViewController.SecondAnswerArray = ThirdAnswerArray.ThirdViewArray

    }
}

答案 1 :(得分:1)

你的正则表达式在第一场比赛时回归。要匹配所有值,请使用g修饰符(全局匹配)。

$re = "/<span class=\"pybar-bg\">.*?width:(.*?)%/sg";

将返回:

100
78

see demo