iphone - How to get matches in iOS using regular expression? -


i got string 'stackoverflow.html' , in regular expression 'stack(.).html' have value in (.).

i find nspredicate like:

nsstring    *string     = @"stackoverflow.html"; nsstring    *expression = @"stack(.*).html"; nspredicate *predicate  = [nspredicate predicatewithformat:@"self matches %@", expression]; bool match = [predicate evaluatewithobject:string] 

but tells got match , doesn't return string, when use nsregularexpression:

nsrange range = [string rangeofstring:expression options:nsregularexpressionsearch|nscaseinsensitivesearch]; if (range.location == nsnotfound) return nil;  nslog (@"%@", [string substringwithrange:(nsrange){range.location, range.length}]); 

it give me total string back, stackoverflow.html, interested in whats in (.*). want 'overflow' back. in php easy do, how can accomplish in xcode ios?

logically if this:

nsinteger firstpartlength  = 5; nsinteger secondpartlength = 5; nslog (@"%@", [string substringwithrange:(nsrange){range.location + firstpartlength, range.length - (firstpartlength + secondpartlength)}] 

it gives me propert result 'overflow'. problem in many cases dont know length of first part or second part. there way can value should in (.*) ?

or must decide choose ugliest method finding location of (.) , calculate first , second part there? in regular expression might have ([a-z]) ugly way of using regular expression location of values between () , use calculate left , right part? , happends if have more? 'a (.) should find answer (.*).' have array result values [0] value after , [1] value after to.

i hope question clear.

thanks in advance,

in ios 4.0+, can use nsregularexpression:

nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"stack(.*).html" options:0 error:null]; nsstring *str = @"stackoverflow.html"; nstextcheckingresult *match = [regex firstmatchinstring:str options:0 range:nsmakerange(0, [str length])]; // [match rangeatindex:1] gives range of group in parentheses // [str substringwithrange:[match rangeatindex:1]] gives first captured group in example 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -