如何通过正则表达式匹配字符串值?

时间:2011-09-09 10:07:18

标签: iphone objective-c ios

我正在制作依赖于html.i的应用程序在代码中传递html文件,并且所有数据都显示在控制台中但我的问题是如何使用正则表达式匹配字符串我没有给出html的所有数据是大的 这是在控制台上打印的值

www.ryanair.com" title="Home">Home</a> | <a href="http://www.ryanair.com/site/EN/faqs.php" title="F.A.Q." target="_blank">F.A.Q.</a> | <a href="http://www.ryanair.com/site/EN/conditions.php?view=privacy" title="Privacy Policy" target="_blank">Privacy Policy</a> | <a href="http://www.ryanair.com/site/EN/conditions.php" title="General Terms &amp; Conditions of Carriage" target="_blank">General Terms &amp; Conditions of Carriage</a> | <a href="http://www.ryanair.com/en/terms-and-conditions/copywrite" title="Terms of Use" target="_blank">Terms of Use</a> | <a href="http://www.ryanair.com/site/EN/faqs.php?sect=CONTACT&amp;div=int_sup" title="Contact Us" target="_blank">Contact Us</a> | <a href="javascript:infoWin('trvfee',0,0,'EN');" title="Fees">Fees</a></div><div id="copyr">Copyright 2009 Ryanair Ltd.</div><input type="hidden" id="language" value=""><input type="hidden" id="culture" value=""><script type="text/javascript"> xtnv=document; xtsd=(window.location.protocol.indexOf('https:')==0?'https://logs152':'http://logc158'); xtsite=(window.location.hostname.indexOf('www.bookryanair.com')!=-1)?'384235':'385193'; xtn2="3"; xtpage="BookingProcess_IE::Search"; xtdi="1";  </script><script type="text/javascript" src="js/Ryanair/xtcore.js"></script><noscript><img width="1" height="1" alt="" src="http://logc158.xiti.com/hit.xiti?s=384235&amp;s2=3&amp;p=BookingProcess_IE::Search&amp;di=1"></noscript></div></div></div><div class="clr"></div><script language="javascript" type="text/javascript"><!--
                    var MacsArray = new Array();

                var SortedStations = new Array(
            "AAR","ABZ","AGA","AHO","ALC","LEI","AOI","BCN","BRI","EGC","SXF","BZR","BIQ","BLL","BHX","BLQ","BOD","BOH","BTS","BRE","BES","BDS","BRS","BSL","BRQ","CRL","BZG","CAG","CCF","CND","ORK","CUF","LDY","DNR","DSA","DUB","MME","NRN","EMA","EDI","EIN","FAO","FEZ","FSC","HHN","FUE","GDN","GOA","GRO","PIK","GSE","GRX","LPA","GRZ","GNB","LBC","HAU","IBZ","XRY","FKB","KTW","KUN","KIR","KLU","NOC","KGS","KRK","SUF","ACE","LPP","LCA","LRH","LBA","LIL","LIG","LNZ","LPL","LCJ","LGW","LTN","STN","LDE","MST","MAD","AGP","MLA","MAN","RAK","MRS","FMM","BGY","MPL","MJV","NDR","NTE","NCL","NQY","FNI","OSI","RYG","TRF","OUD","PMO","PMI","BVA","MMX","XCR","PMF","PGF","PEG","PSR","PSA","PDV","PIS","OPO","POZ","PRG","PUY","REU","RHO","RIX","RMI","RDZ","CIA","RZE","SZG","SDR","SCQ","SVQ","SNN","VXO","EBU","NYO","VST","SZZ","TMP","TNG","TFS","TLN","TUF","TPS","TRS","TRN","VLC","VLL","TSF","VRN","VOL","WRO","ZAD","ZAZ","RLG","KSD","CSO","SFT","TLL","HUY","RJK","SKG","VCE","VNO","CHQ","CFU","LEJ"
                );

                var Stations = new Array();
                Stations["AAR"] = new Station("AAR", "", "Aarhus", true, true, new Array("AGP","RYG","STN"));
Stations["AGP"] = new Station("AGP", "", "Malaga", true, true, new Array("AAR","BCN","BGY","BHX","BLL","BLQ","BOH","BRE","BRS","BTS","BVA","CRL","CSO","DUB","EDI","EIN","EMA","FMM","GSE","HAU","HHN","IBZ","KRK","LBA","LPL","MAN","MMX","MRS","MST","NRN","NYO","ORK","PIK","PSA","RYG","SCQ","SDR","SNN","STN","SXF","TMP","TRF","VCE","VLC","VLL","VST","WRO","ZAZ"));
Stations["RYG"] = new Station("RYG", "", "Oslo (Rygge)", true, true, new Array("AAR","AGP","ALC","BCN","BVA","BZR","CIA","CRL","DUB","FAO","FMM","GDN","HHN","KGS","KRK","KUN","LGW","LPL","LRH","MAD","MAN","NCL","NYO","PMI","PMO","POZ","RIX","SKG","STN","SXF","TLL","TMP","VCE","VLC","WRO","XCR","ZAD"));


i done this for match all string value in code
- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    [self sendRequest ];
    //Extraction of valid mail ids
    //NSString * searchString = @" para.g@gmail.com sourabh_84368@gmail.com abc@def.in abcd@yahoomail.com mobisoft@mail.in andy@rediffmail.com";
    NSString * searchString = loginstatus;
    NSString *regexString = @"[a-z0-9_.%]+@[a-z0-9_.%]+\\.[a-z][a-z][a-z]";
    //NSString *regexString = @"Stations\["(.*)"\] = new Station\((.*)\);";
    NSArray  *matchArray   = nil;
    matchArray = [searchString componentsMatchedByRegex:regexString];
    NSLog(@"matchArray: %@", matchArray);
    // Override point for customization after application launch
    //NSString* source = @"Test;12;Y";
//  NSArray* columns = [source componentsSeparatedByRegex:@";\\s*"];
//  NSLog([columns description]);
//    [window makeKeyAndVisible];
}

请求有人帮助我

3 个答案:

答案 0 :(得分:1)

使用NSPredicate将字符串与正则表达式匹配。

NSString *searchString = @"loginStatus";
NSString *regexString = @"[a-z0-9_.%]+@[a-z0-9_.%]+\\.[a-z][a-z][a-z]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@", regexString];
BOOL isStringValid = [predicate evaluateWithObject:searchString];

答案 1 :(得分:0)

  • 如果您正在开发iOS4 +,请使用NSRegularExpression
  • 如果您需要定位以前的iOS版本:
    • 您可以尝试使用NSPredicate,它通过MATCHES运算符支持ICU RegEx语法。但它只允许你测试字符串是否匹配,不能做替换也不能提取匹配的字符串,所以这是非常有限的。
    • 您可以使用RegExKit
    • 等第三方库

答案 2 :(得分:0)

如果您尝试使用正则表达式来解析HTML,那么您可能会采用错误的方式。已有专门的解析器,例如NSXMLParser,您可以更轻松地在应用程序中使用,而不是重新发明轮子编写正则表达式以从HTML中获取数据。