Return 一个字符串的所有出现
Return all occurrences of a string
我正在尝试弄清楚如何从一个更大的字符串中获取字符串的每次出现。
较大字符串的示例:
"xmp_id": 3243041, "certified": 1,"xmp_id": 3243042, "certified":
1,"xmp_id": 3243043, "certified": 1,"xmp_id": 3243044, "certified":
1,"xmp_id": 3243045, "certified": 1,"xmp_id": 3243046, "certified":
1,"xmp_id": 3243047, "certified": 1,"xmp_id": 3243048, "certified":
1,"xmp_id": 3243049, "certified": 1,"xmp_id": 3243050, "certified":
1,"xmp_id": 3243051, "certified": 1,"xmp_id": 3243052, "certified": 1,
在 VB.Net 中,我会使用类似这样的方法来获取每个 "xmp_id":
的值
Dim inputString As String = RichTextBox1.Text
Dim pattern As String = "(?<=\<b1\>).+?(?=\<\/b1\>)"
Dim col As MatchCollection = Regex.Matches(inputString, pattern)
For Each match As Match In col
Console.WriteLine(match.Groups(1).Value)
Next
我到处搜索,没能找到与 VB.Net 代码等效的内容。我想获得 "xmp_ID": 和 、"certified": 之间的字符串 任何人都知道如何去Swift?
关于这个
Swift 没有自己的正则表达式支持,但您可以使用 Foundation 的 NSRegularExpression
class。它产生 NSTextCheckingResult
个对象。将 Swift 的 String
类型与 NSRegularExpression
和 NSTextCheckingResult
一起使用有点痛苦,因此首先将您的输入字符串转换为 NSString
.
let text = "\"xmp_id\": 3243041, \"certified\": 1,\"xmp_id\": 3243042, \"certified\": 1,\"xmp_id\": 3243043, \"certified\": 1,\"xmp_id\": 3243044, \"certified\": 1,\"xmp_id\": 3243045, \"certified\": 1,\"xmp_id\": 3243046, \"certified\": 1,\"xmp_id\": 3243047, \"certified\": 1,\"xmp_id\": 3243048, \"certified\": 1,\"xmp_id\": 3243049, \"certified\": 1,\"xmp_id\": 3243050, \"certified\": 1,\"xmp_id\": 3243051, \"certified\": 1,\"xmp_id\": 3243052, \"certified\": 1," as NSString
let rx = NSRegularExpression(pattern: "\"xmp_id\": ([0-9]+)", options: nil, error: nil)!
let range = NSMakeRange(0, text.length)
let matches = rx.matchesInString(text, options: nil, range: range)
for matchObject in matches {
let match = matchObject as NSTextCheckingResult
let range = match.rangeAtIndex(1)
let value = text.substringWithRange(range)
println(value)
}
我正在尝试弄清楚如何从一个更大的字符串中获取字符串的每次出现。
较大字符串的示例:
"xmp_id": 3243041, "certified": 1,"xmp_id": 3243042, "certified":
1,"xmp_id": 3243043, "certified": 1,"xmp_id": 3243044, "certified":
1,"xmp_id": 3243045, "certified": 1,"xmp_id": 3243046, "certified":
1,"xmp_id": 3243047, "certified": 1,"xmp_id": 3243048, "certified":
1,"xmp_id": 3243049, "certified": 1,"xmp_id": 3243050, "certified":
1,"xmp_id": 3243051, "certified": 1,"xmp_id": 3243052, "certified": 1,
在 VB.Net 中,我会使用类似这样的方法来获取每个 "xmp_id":
的值Dim inputString As String = RichTextBox1.Text
Dim pattern As String = "(?<=\<b1\>).+?(?=\<\/b1\>)"
Dim col As MatchCollection = Regex.Matches(inputString, pattern)
For Each match As Match In col
Console.WriteLine(match.Groups(1).Value)
Next
我到处搜索,没能找到与 VB.Net 代码等效的内容。我想获得 "xmp_ID": 和 、"certified": 之间的字符串 任何人都知道如何去Swift?
关于这个Swift 没有自己的正则表达式支持,但您可以使用 Foundation 的 NSRegularExpression
class。它产生 NSTextCheckingResult
个对象。将 Swift 的 String
类型与 NSRegularExpression
和 NSTextCheckingResult
一起使用有点痛苦,因此首先将您的输入字符串转换为 NSString
.
let text = "\"xmp_id\": 3243041, \"certified\": 1,\"xmp_id\": 3243042, \"certified\": 1,\"xmp_id\": 3243043, \"certified\": 1,\"xmp_id\": 3243044, \"certified\": 1,\"xmp_id\": 3243045, \"certified\": 1,\"xmp_id\": 3243046, \"certified\": 1,\"xmp_id\": 3243047, \"certified\": 1,\"xmp_id\": 3243048, \"certified\": 1,\"xmp_id\": 3243049, \"certified\": 1,\"xmp_id\": 3243050, \"certified\": 1,\"xmp_id\": 3243051, \"certified\": 1,\"xmp_id\": 3243052, \"certified\": 1," as NSString
let rx = NSRegularExpression(pattern: "\"xmp_id\": ([0-9]+)", options: nil, error: nil)!
let range = NSMakeRange(0, text.length)
let matches = rx.matchesInString(text, options: nil, range: range)
for matchObject in matches {
let match = matchObject as NSTextCheckingResult
let range = match.rangeAtIndex(1)
let value = text.substringWithRange(range)
println(value)
}