1.正則表達式的語法
2.正則表達式不常用的語法
Q:經常看見的正則前面的 (?i) (?s) (?m) (?is) (?im) 是什么意思?
A: 稱為內聯匹配模式,通常用內聯匹配模式代替使用枚舉值RegexOptions指定的全局匹配模式,寫起來更簡潔。
(?i) 表示所在位置右側的表達式開啟忽略大小寫模式
(?s) 表示所在位置右側的表達式開啟單行模式。 更改句點字符 (.) 的含義,以使它與每個字符(而不是除 \n 之外的所有字符)匹配。
注意:
(?s)通常在匹配有換行的文本時使用
(?m) 表示所在位置右側的表示式開啟指定多行模式。 更改 ^ 和 $ 的含義,以使它們分別與任何行的開頭和結尾匹配, 而不只是與整個字符串的開頭和結尾匹配。
注意:
(?m)只有在正則表達式中涉及到多行的“^”和“$”的匹配時,才使用Multiline模式。
上面的匹配模式可以組合使用,比如(?is),(?im)。 另外,還可以用(?i:exp)或者(?i)exp(?-i)來指定匹配的有效范圍。
(?<=)零寬度正回顧后發斷言。
僅當子表達式在此位置的左側匹配時才繼續匹配。例如,(?<=19)99 與跟在 19 后面的 99 的實例匹配。此構造不會回溯。
3.ios oc 正則表達式分割字符串為字符串數組
//初始化一個 NSRegularExpression 對象 注:_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 是每個和表達式匹配好的字符串。
str1 = [_str substringWithRange:b.range];
NSLog(@" str 1 is %@",str1);
}
//獲得匹配的字符串的個數
NSUInteger numberOfMatches = [regex numberOfMatchesInString:_str options:0 range:NSMakeRange(0, [_str length])];
//替換匹配的字符串 $0很重要 $0不行的話 $1依此類推 打印了看結果
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.正則表達式獲得WebView內容按段落分割為數組
//解析字符串
-(NSArray *)getZZwithString:(NSString *)string{
NSArray *strArray = nil;
if (string.length == 0) {
return strArray;
}
//初步去除HTML標簽
NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>| " options:0 error:nil];
string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
//去除剩余尖括號(-->)之間的內容(由于尖括號內有換行符)
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:@""];
//去除尾部多余內容
string = [string stringByReplacingOccurrencesOfString:@"jQuery(document).ready(function(){" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"App.init();" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"});" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
//拆分為數組
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內容
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[SVProgressHUD dismiss];
UIWebView *web = webView;
//獲取所有的html
NSString *allHtml = @"document.documentElement.innerHTML";
//獲取網頁title
NSString *htmlTitle = @"document.title";
//獲取網頁的一個值
NSString *htmlNum = @"document.getElementById('title').innerText";
//獲取到得網頁內容
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結果在這---%@",currentURL);
//轉換內容
self.strArray = [self getZZwithString:self.htmlStr];
for (NSString *str in self.strArray) {
NSLog(@"檢查檢查檢查----%@",str);
}
}