2012-12-19 5 views
1
html로 40 개 문자의 ID의 발생을 찾기 위해 NSRegularExpression를 사용하여

IOS는 HTML에서 ID를 찾을 정규식을 사용

여기

내 코드 : 내 현재 정규식 그래서

- (NSString *)stripOutHttp:(NSString *)string { 

NSLog(@"the page content :: %@", string); 

// Setup an NSError object to catch any failures 
NSError *error = NULL; 

// create the NSRegularExpression object and initialize it with a pattern 
// the pattern will match any http or https url, with option case insensitive 

//search for:: <input type="hidden" name="XID" value="f3f3fbafe552358d9312d1fe30670add09adc36c" /> 


NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<input type=\"hidden\" name=\"XID\" value\"?" options:NSRegularExpressionCaseInsensitive error:&error]; // ultimo funcional 



// try /\b([a-f0-9]{40})\b/ 


// create an NSRange object using our regex object for the first match in the string 

NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; 

// check that our NSRange object is not equal to range of NSNotFound 

if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) { 
    // Since we know that we found a match, get the substring from the parent string by using our NSRange object 

    NSString *substringForFirstMatch = [string substringWithRange:rangeOfFirstMatch]; 

    NSLog(@"Extracted data : %@",substringForFirstMatch); 

    // return the matching string 
    return substringForFirstMatch; 
} 

return NULL; 
    } 

:

Extracted data : <input type="hidden" name="XID" value 
:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<input type=\"hidden\" name=\"XID\" value\"?" options:NSRegularExpressionCaseInsensitive error:&error]; // ultimo funcional 

은 내가 필요한 부분을 얻을

이제 40 자 값에 대한 응답을 얻으려면 어떻게해야합니까?

나는

는 ::

<input type="hidden" name="XID" value="f3f3fbafe552358d9312d1fe30670add09adc36c" /> 

덕분에 많은 후 응답 메신저의 종류,

// try /\b([a-f0-9]{40})\b/ 

으로 시도했지만 아직 그것을 사용하는 방법을 이해하는 것 해달라고

+2

HTML 구문 분석기로 HTML을 구문 분석하십시오. 훨씬 더 쉬울 것입니다. – Blender

답변

1

정규식

<input type=\"hidden\" name=\"XID\" value=\"([a-f0-9]{40})\"[\s]*/> 

:)하지만 지금, 당신의 질문에 대한 답은 다음이 될 것입니다 하지만, 한 가지 점은 임의의 공백이 가능한 많은 공간을 사용한다는 것입니다. 내가 당신이라면 HTML 파서 라이브러리를 살펴볼 것입니다.

2

HTML 또는 xml 파서로 전체 구문 분석을 고려해야합니다 (예 : Blender

가 나는 그것이 좋은 생각이라고 생각하지 않습니다 귀하의 입력 문자열과 일치해야합니다

"<[^>]*id=DIVNAME.*?>(.*?)/>" 
관련 문제