3
我使用這個代碼來解析NSString
:的iOS拆分NSString的正則表達式
NSString *string = @"{{nat fs g player|no=1|pos=GK|name=[[Hugo Lloris]]|age={{Birth date and age|1986|12|26|df=y}}|caps=73|goals=0|club=[[Tottenham Hotspur F.C.|Tottenham Hotspur]]|clubnat=ENG}}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{nat fs g player\\|no=(.*)\\|pos=(.*?)\\|name=\\[\\[(.*?)\\]\\]\\|age=\\{\\{Birth date and age\\|(.*?)\\|(.*?)\\|(.*?)\\|df=y\\}\\}\\|caps=(.*?)\\|goals=(.*?)\\|club=\\[\\[(.*?)\\|(.*)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
NSString *a = [string substringWithRange:[match rangeAtIndex:0]];
NSString *b = [string substringWithRange:[match rangeAtIndex:1]];
NSString *c = [string substringWithRange:[match rangeAtIndex:2]];
NSString *d = [string substringWithRange:[match rangeAtIndex:3]];
NSString *e = [string substringWithRange:[match rangeAtIndex:4]];
NSString *f = [string substringWithRange:[match rangeAtIndex:5]];
NSString *g = [string substringWithRange:[match rangeAtIndex:6]];
NSString *h = [string substringWithRange:[match rangeAtIndex:7]];
NSString *i = [string substringWithRange:[match rangeAtIndex:8]];
NSString *j = [string substringWithRange:[match rangeAtIndex:9]];
NSString *k = [string substringWithRange:[match rangeAtIndex:10]];
}
有了這個代碼,我真的得到數據,但是當我試圖用相同的模式拆分2串:
NSString *string = @"{{nat fs g player|no=1|pos=GK|name=[[Hugo Lloris]]|age={{Birth date and age|1986|12|26|df=y}}|caps=73|goals=0|club=[[Tottenham Hotspur F.C.|Tottenham Hotspur]]|clubnat=ENG}}{{nat fs g player|no=16|pos=GK|name=[[Steve Mandanda]]|age={{Birth date and age|1985|3|28|df=y}}|caps=21|goals=0|club=[[Olympique de Marseille|Marseille]]|clubnat=FRA}}";
我只得到一個匹配,任何想法爲什麼?
@Priya這是另外一個問題,在我看來,這是過於寬泛的話題。隨意發佈一個問題,但不要忘記提供*確切的模式要求*。沒有唯一的地址驗證正則表達式。 –
@WiktorStribiżew...我無法驗證以下地址: // NSString * strAddress = @「No.9,Arulambal Street,T.Nagar,Chennai - 600017」; //無效 // * NSString的strAddress = @ 「12號,channiah角落,Singaiyanapalya,下一步VR商城,馬哈德瓦普拉,班加羅爾,卡納塔克邦560048」; //無效 我怎麼能驗證上述地址 – Priya
@Priya *我懷疑*任何人*都可以。 –