正则表达式模式匹配返回结果 [英] Return results from regular expression pattern matching

查看:76
本文介绍了正则表达式模式匹配返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串(在本例中为 HTML),其中包含用于显示体育比赛结果的相同模式.所以,HTML 标签是已知的,但每个游戏的值是未知的.

I have a string (HTML in this example case) which contains the same pattern for displaying the results of sports games. So, the HTML tags are known, but the values for each game are not.

在 Perl 中,我们可以这样做:

In Perl, we can do this:

if ( $content =~ /\<\/a\>\<br\>(\d+)\<\/span\>\<br\>(\d+)\-(\d+).+\<\/a\>\<br\>(\d+)\<\/span\>\<br\>(\d+)\-(\d+)/) {
  $visitingTeamScore = $1; // $1 is the 1st matched digit
  $visitingTeamWins = $2; // $2 is the 2nd matched digit
  $visitingTeamLosses = $3; // Etc
  $homeTeamScore = $4;
  $homeTeamWins = $5;
  $homeTeamLosses = $6;
}

返回括号内的数字,在本例中为 6 个不同数字长度的整数.然后我们可以将这些匹配项分配给变量.

which returns the digits inside the parentheses, in this case 6 total integers of varying digit lengths. We can then assign those matches to variables.

<小时>

来自这个问题的答案:Swift Get string between字符串中有 2 个字符串,我有以下 Swift 代码:


From an answer in this question: Swift Get string between 2 strings in a string, I have the following Swift code:

extension String {
    func sliceFrom(start: String, to: String) -> String? {
        return (rangeOfString(start)?.endIndex).flatMap { sInd in
            (rangeOfString(to, range: sInd..<endIndex)?.startIndex).map { eInd in
                substringWithRange(sInd..<eInd)
            }
        }
    }
}

let firstMatch = content?.sliceFrom("</a><br>", to: "</span>") // The first integer in the string

问题出现在获得第 4 个整数时,该整数也在 </a\><br></span> 之间,因此结果匹配将再次成为第一个数字.

The problem comes in when getting the 4th integer which is also between </a\><br> and </span> so the resulting match will be the first digit again.

我可以手动计算字符数(这本身并不是一门完美的科学,因为每个整数中的数字可能不同)来做一些难看的事情:

I can manually count the characters (which itself isn't a perfect science because the digits in each integer can differ) to do something ugly like:

let newRawHTML = content![content!.startIndex.advancedBy(15)...content!.startIndex.advancedBy(5)]

另一种可能性是从字符串中删除已经匹配的任何内容,使其在每次后续搜索时都更短(我不确定如何实现.)这样做的方法是什么?Swift 中有没有办法提取"匹配项?

Another possibility is to remove anything matched already from the string, making it shorter for each subsequent search (which I'm not sure how to implement.) What's the way to do this? Is there any way in Swift to "pluck out" the matches?

推荐答案

您作为 Perl 示例显示的代码使用了正则表达式.如果模式变得有点复杂,你最好直接使用 NSRegularExpression.

The code you have shown as a Perl example, uses regular expression. And in case the pattern is getting a little bit complex, you'd better use NSRegularExpression directly.

let pattern = "</a><br>(\\d+)</span><br>(\\d+)-(\\d+).+</a><br>(\\d+)</span><br>(\\d+)-(\\d+)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
if let match = regex.firstMatchInString(content, options: [], range: NSRange(0..<content.utf16.count)) {
    let visitingTeamScore = (content as NSString).substringWithRange(match.rangeAtIndex(1))
    let visitingTeamWins = (content as NSString).substringWithRange(match.rangeAtIndex(2))
    let visitingTeamLosses = (content as NSString).substringWithRange(match.rangeAtIndex(3))
    let homeTeamScore = (content as NSString).substringWithRange(match.rangeAtIndex(4))
    let homeTeamWins = (content as NSString).substringWithRange(match.rangeAtIndex(5))
    let homeTeamLosses = (content as NSString).substringWithRange(match.rangeAtIndex(6))
    //...use the values
}

这篇关于正则表达式模式匹配返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆