Rocky
Rocky

Reputation: 1423

How to match string value by regular expression?

i am making application which depend on html.i pass the html file in code and all data are display in console but my problem is that how to match string by using regular expression i am not giving all data of html is to large this is value which is print on console

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 = @" [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]";
    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];
}

pleas some one help me on this

Upvotes: 1

Views: 587

Answers (3)

Abizern
Abizern

Reputation: 150595

If you are trying to use regular expressions to parse the HTML you may be going about it the wrong way. There are dedicated parsers already for this, such as NSXMLParser which will be easier for you to use in your app than re-inventing the wheel writing Regular expressions to get data out of HTML.

Upvotes: 0

AliSoftware
AliSoftware

Reputation: 32681

  • If you are developing for iOS4+, use the NSRegularExpression class
  • If you need to target previous iOS releases:
    • you may try using NSPredicate, which supports ICU RegEx syntax thru the MATCHES operator. But it only allows you to test if the string match, and can't do replacements nor extract matching strings, so this is quite limited.
    • You can use Third-Party libraries like RegExKit

Upvotes: 0

EmptyStack
EmptyStack

Reputation: 51374

Use NSPredicate to match string against a regular expression.

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];

Upvotes: 1

Related Questions