Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 利用swift中的正则表达式从句子内部提取单词_Regex_Swift_Nsregularexpression - Fatal编程技术网

Regex 利用swift中的正则表达式从句子内部提取单词

Regex 利用swift中的正则表达式从句子内部提取单词,regex,swift,nsregularexpression,Regex,Swift,Nsregularexpression,我想要一个正则表达式从字符串中提取Starboy和Weekend/Daft朋克: The Weeknd / Daft Punk - text=\"Starboy\" song_spot=\"M\" MediaBaseId=\"2238986\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"744880\" TPID=\"43758958\" cartcutId=\"08 到目前为止,这是我的尝试 do {

我想要一个正则表达式从字符串中提取
Starboy
Weekend/Daft朋克

The Weeknd / Daft Punk - text=\"Starboy\" song_spot=\"M\" MediaBaseId=\"2238986\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"744880\" TPID=\"43758958\" cartcutId=\"08
到目前为止,这是我的尝试

do {  
    let input = "The Weeknd / Daft Punk - text=\"Starboy\" song_spot=\"M\" MediaBaseId=\"2238986\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"744880\" TPID=\"43758958\" cartcutId=\"0893584001\""  
    let regex = try NSRegularExpression(pattern: "text=\"(.*)", options: NSRegularExpression.Options.caseInsensitive)  
    let matches = regex.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))  

    if let match = matches.first {  
        let range = match.range(at:1)  
        if let swiftRange = Range(range, in: input) {  
            let name = input[swiftRange]  
            print(name)  
        }  
    }  
} catch {  
    print("Regex was bad!")  
}  
但这给了我全部的线索

Starboy" song_spot="M" MediaBaseId="2238986" itunesTrackId="0" amgTrackId="-1" amgArtistId="0" TAID="744880" TPID="43758958" cartcutId="0893584001"

如果您需要捕获序列
-text=
之前的所有文本以及引号之间的任何单词,您可以使用此正则表达式
“*(?=(text=\”[\\w\\s]+\”)”
并捕获序列
text=“
之后的任何单词,您可以使用此正则表达式
”(?如果您需要捕获序列
-text=
之前的所有文本以及引号之间的任何单词,您可以使用此正则表达式*(?=(text=\“[\\w\\s]+\”)”)
并捕获序列
text=“
之后的任何单词,您可以使用此正则表达式
”(?您拼写的“Weekend”不正确(还有其他问题)你需要著名的“最小匹配”-谷歌搜索!享受你拼写错误的“周末”(以及其他问题)你需要著名的“最小匹配”-谷歌搜索!享受
let string = """
The Weeknd / Daft Punk - text=\"Starboy\" song_spot=\"M\" MediaBaseId=\"2238986\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"744880\" TPID=\"43758958\" cartcutId=\"08
"""
let pattern = ".*(?=( - text=\"[\\w\\s]+\"))|(?<=text=\")([\\w\\s]+)"

do {
    let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
    let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count))
    for match in matches {
        if let range = Range(match.range, in: string) {
            let name = string[range]
            print(name)
        }
    }
} catch {
    print("Regex was bad!")
}
The Weeknd / Daft Punk
Starboy