【发布时间】:2016-08-05 23:33:44
【问题描述】:
我只想提取以下代码后的网站链接:
import UIKit
import Foundation
func regMatchGroup(regex: String, text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
var internalString = [String]()
for result in results {
for var i = 0; i < result.numberOfRanges; ++i{
internalString.append(nsString.substringWithRange(result.rangeAtIndex(i)))
}
}
return internalString
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
// USAGE:
let textsearch = "mohamed amine ammach <img alt='http://fb.com' /> hhhhhhhhhhh <img alt='http://google.com' />"
let matches = regMatchGroup("alt='(.*?)'", text: textsearch)
if (matches.count > 0) // If we have matches....
{
for (var i=0;i < matches.count;i++) {
print(matches[i])
}
}
游乐场打印以下内容:
alt='http://fb.com'
http://fb.com
alt='http://google.com'
http://google.com
但我只想得到:
http://fb.com
http://google.com
有人可以帮我解决这个问题吗?,我将不胜感激
【问题讨论】:
-
您的结果在第一个捕获组中
标签: regex swift string extract