1.正則表達(dá)式的語法
2.正則表達(dá)式不常用的語法
Q:經(jīng)巢艄冢看見的正則前面的 (?i) (?s) (?m) (?is) (?im) 是什么意思?
A: 稱為內(nèi)聯(lián)匹配模式地粪,通常用內(nèi)聯(lián)匹配模式代替使用枚舉值RegexOptions指定的全局匹配模式咕宿,寫起來更簡(jiǎn)潔。
(?i) 表示所在位置右側(cè)的表達(dá)式開啟忽略大小寫模式
(?s) 表示所在位置右側(cè)的表達(dá)式開啟單行模式。 更改句點(diǎn)字符 (.) 的含義错负,以使它與每個(gè)字符(而不是除 \n 之外的所有字符)匹配恭金。
注意:
(?s)通常在匹配有換行的文本時(shí)使用
(?m) 表示所在位置右側(cè)的表示式開啟指定多行模式操禀。 更改 ^ 和 $ 的含義,以使它們分別與任何行的開頭和結(jié)尾匹配横腿, 而不只是與整個(gè)字符串的開頭和結(jié)尾匹配颓屑。
注意:
(?m)只有在正則表達(dá)式中涉及到多行的“^”和“$”的匹配時(shí),才使用Multiline模式耿焊。
上面的匹配模式可以組合使用揪惦,比如(?is),(?im)。 另外罗侯,還可以用(?i:exp)或者(?i)exp(?-i)來指定匹配的有效范圍器腋。
(?<=)零寬度正回顧后發(fā)斷言。
僅當(dāng)子表達(dá)式在此位置的左側(cè)匹配時(shí)才繼續(xù)匹配钩杰。例如纫塌,(?<=19)99 與跟在 19 后面的 99 的實(shí)例匹配。此構(gòu)造不會(huì)回溯榜苫。
3.ios oc 正則表達(dá)式分割字符串為字符串?dāng)?shù)組
//初始化一個(gè) NSRegularExpression 對(duì)象 注:_str是要匹配的字符串
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http://([\\\\w-]+\\\\.)+[\\\\w-]+(/[\\\\w- ./?%&=]*)?" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *array = nil;
array = [regex matchesInString:_str options:0 range:NSMakeRange(0, [_str length])];
NSString *str1 = nil;
for (NSTextCheckingResult* b in array)
{
str1 是每個(gè)和表達(dá)式匹配好的字符串护戳。
str1 = [_str substringWithRange:b.range];
NSLog(@" str 1 is %@",str1);
}
//獲得匹配的字符串的個(gè)數(shù)
NSUInteger numberOfMatches = [regex numberOfMatchesInString:_str options:0 range:NSMakeRange(0, [_str length])];
//替換匹配的字符串 $0很重要 $0不行的話 $1依此類推 打印了看結(jié)果
NSString *modifiedString = [regex stringByReplacingMatchesInString:_str
options:0
range:NSMakeRange(0, [_str length])
withTemplate:@"<a href=\\"$0\\">$0</a>"];
NSLog(@"######## the modified string is %@",modifiedString);
4.正則表達(dá)式獲得WebView內(nèi)容按段落分割為數(shù)組
//解析字符串
-(NSArray *)getZZwithString:(NSString *)string{
NSArray *strArray = nil;
if (string.length == 0) {
return strArray;
}
//初步去除HTML標(biāo)簽
NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>| " options:0 error:nil];
string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
//去除剩余尖括號(hào)(-->)之間的內(nèi)容(由于尖括號(hào)內(nèi)有換行符)
NSRegularExpression *regularExp = [NSRegularExpression regularExpressionWithPattern:@"(?is)(?<=-->).*?(?=s-->)" options:0 error:nil];
string = [regularExp stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
string = [string stringByReplacingOccurrencesOfString:@"-->s-->" withString:@""];
//去除多余空格
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
//去除尾部多余內(nèi)容
string = [string stringByReplacingOccurrencesOfString:@"jQuery(document).ready(function(){" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"App.init();" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"});" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
//拆分為數(shù)組
NSRegularExpression *regularEx=[NSRegularExpression regularExpressionWithPattern:@".+(?=\\n)" options:0 error:nil];
NSArray * matches = [regularEx matchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *check in matches) {
NSString *str = [string substringWithRange: check.range];
// NSLog(@"這里這里---%@", str);
[tempArray addObject:str];
}
strArray = tempArray;
return strArray;
}
//UIWebViewDelegate 加載完畢獲取HTML內(nèi)容
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[SVProgressHUD dismiss];
UIWebView *web = webView;
//獲取所有的html
NSString *allHtml = @"document.documentElement.innerHTML";
//獲取網(wǎng)頁title
NSString *htmlTitle = @"document.title";
//獲取網(wǎng)頁的一個(gè)值
NSString *htmlNum = @"document.getElementById('title').innerText";
//獲取到得網(wǎng)頁內(nèi)容
NSString *allHtmlInfo = [web stringByEvaluatingJavaScriptFromString:allHtml];
// NSLog(@"allHtmlInfo====%@",allHtmlInfo);
NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlTitle];
NSLog(@"htmlTitle=====%@",titleHtmlInfo);
NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlNum];
NSLog(@"numHtmlInfo===%@",numHtmlInfo);
self.htmlStr = allHtmlInfo;
// NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
// NSLog(@"url結(jié)果在這---%@",currentURL);
//轉(zhuǎn)換內(nèi)容
self.strArray = [self getZZwithString:self.htmlStr];
for (NSString *str in self.strArray) {
NSLog(@"檢查檢查檢查----%@",str);
}
}