Regex NSRegularExpression,带Swift中的模板

Regex NSRegularExpression,带Swift中的模板,regex,swift,cocoa,Regex,Swift,Cocoa,我正在用代码获取对HTTP POST请求的响应 let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {data, response, error in if error != nil { println("error=\(error)") return } // Get the response to the HTTP POST var responseString = NSString(data: data

我正在用代码获取对HTTP POST请求的响应

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {data, response, error in

if error != nil {
println("error=\(error)")
return
}
// Get the response to the HTTP POST
var responseString = NSString(data: data, encoding: NSUTF8StringEncoding)!}
task.resume()
我试图定义两个正则表达式

let regex0: NSRegularExpression = NSRegularExpression(pattern: "<b>District Representatives:</b>", options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: nil)!

let regex1: NSRegularExpression = NSRegularExpression(pattern: "\\A.*<b>District Representatives:</b>.*href=\"http://www\.sec\.state\.ma\.us/ele/eledist/con11idx\.htm#D[1-9]\" target=\"_blank\">(.*?)</a>.*href=\"http://www\.sec\.state\.ma\.us/ele/eledist/sen11idx\.htm#[0-9]{0,5}[a-z]{1,20}\" target=\"_blank\">(.*?)</a>.*\"http://www\.sec\.state\.ma\.us/ele/eledist/reps11idx\.htm#[a-z]{1,13}[0-9]{0,2}\" target=\"_blank\">(.*?)</a>.*\\z", options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: nil)!
但我收到了错误消息“使用未解析标识符responseString”

我想知道如何测试正则表达式匹配是否成功。在本例中,我可以测试responseString是否包含我的测试字符串。这似乎可以很好地处理Swift字符串,但我无法将其用于NSString

我认为我的regex1正则表达式中的模式是可以的,因为我在TextWrangler中测试了等效的模式。我在那里使用的模式是

(?s)\A.*<b>District Representatives:</b>.*href="http://www\.sec\.state\.ma\.us/ele/eledist/con11idx\.htm#D[1-9]" target="_blank">(.*?)</a>.*href="http://www\.sec\.state\.ma\.us/ele/eledist/sen11idx\.htm#[0-9]{0,5}[a-z]{1,20}" target="_blank">(.*?)</a>.*"http://www\.sec\.state\.ma\.us/ele/eledist/reps11idx\.htm#[a-z]{1,13}[0-9]{0,2}" target="_blank">(.*?)</a>.*\z
但这也不起作用


我天真地认为,既然我已经使用正则表达式30年了,这一部分就很容易了。显然不是。

您的
regex1
在文字点上缺少双转义符(请参见
www.sec\.state\.ma\.us

正确的regex1声明将是

@"\\A.*<b>District Representatives:</b>.*href=\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/con11idx\\.htm#D[1-9]\" target=\"_blank\">(.*?)</a>.*href=\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/sen11idx\\.htm#[0-9]{0,5}[a-z]{1,20}\" target=\"_blank\">(.*?)</a>.*\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/reps11idx\\.htm#[a-z]{1,13}[0-9]{0,2}\" target=\"_blank\">(.*?)</a>.*\\z"
@“\\A.*地区代表:.*href=\”http://www\\.sec\\.state\\.ma\\.us/ele/eledist/con11idx\\.htm#D[1-9]\“target=\”\u blank\“>(.*).*href=\”http://www\\.sec\\.state\\.ma\\.us/ele/eledist/sen11idx\\.htm#[0-9]{0,5}[a-z]{1,20}\'target=\'u blank\'>(.*http://www\\.sec\\.state\\.ma\\.us/ele/eledist/reps11idx\\.htm#[a-z]{1,13}[0-9]{0,2}\“目标=\”\u blank\“>(.*).*\\z”

您使用未解析标识符responseString可能是因为您在声明和使用
numberOfMatches
的方法之外声明了
responseString

谢谢。该修复程序消除了regex1定义中的错误消息。“未解析标识符responseString的使用”“这个问题似乎是我剩下的许多困难的根源。我想我必须先解决这个问题,然后才能继续处理正则表达式问题。请用regexp和reponseString发布完整的代码,或者如果太长,请使用下面的答案。这有用吗?谢谢@Stribizev的帮助。我把当前的程序(减去一些垃圾)放在Pastenbin上,然后我添加了关于当前绊脚石的注释,程序中有一些注释描述了我尚未编写的部分。在当时的问题中,我试图给出我的程序模板(不包括做有用工作的部分)。现在,我的主要问题是我希望有一个同步循环,而Xcode希望我有异步任务。
responseString.replaceMatchesInString(options: nil, range: NSMakeRange(0, responseString.length), withTemplate template: "$1\t$2\t$3")
@"\\A.*<b>District Representatives:</b>.*href=\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/con11idx\\.htm#D[1-9]\" target=\"_blank\">(.*?)</a>.*href=\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/sen11idx\\.htm#[0-9]{0,5}[a-z]{1,20}\" target=\"_blank\">(.*?)</a>.*\"http://www\\.sec\\.state\\.ma\\.us/ele/eledist/reps11idx\\.htm#[a-z]{1,13}[0-9]{0,2}\" target=\"_blank\">(.*?)</a>.*\\z"