2015-01-18 4 views
-1

훨씬 큰 문자열에서 각 문자열을 가져 오는 방법을 파악하려고합니다. 큰 문자열의문자열의 모든 항목을 반환하십시오.

예 :이 같은 것을 사용하는 것이 VB.Net에서

"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, 

각 "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에서이 문제를 해결하는 방법을 알고 싶습니까?

+0

나는 얇은이'(? <= \ "xmp_ID \": \ s +). *? (, \ s + \ "인증 \":)'작동 할 수 있습니다. –

+0

어떻게이 모든 반복을 얻으려면이 루핑 갈까요? 나는 또한 적어도 하나를 찾습니다 있는지 확인하기 위해 이것을 점검 할 것입니다. – BooMSticK

답변

1

Swift는 정규 표현식을 지원하지 않지만 Foundation의 NSRegularExpression 클래스를 사용할 수 있습니다. NSTextCheckingResult 개체를 생성합니다. Swift의 String 유형을 NSRegularExpressionNSTextCheckingResult과 함께 사용하는 것은 고통 스러울 수 있으므로 먼저 입력 문자열을 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) 
} 
관련 문제