YosiFZ
YosiFZ

Reputation: 7900

Regex get string from string

I have NSString that contain this string :

c&&(b.signature=Rk(c));return ql(a,b)}

The RK can be any two chars.

I try to get the RK from the string with (RegexKitLit):

NSString *functionCode = [dataStr2 stringByMatching:@".signature=(.*?)\(" capture:1L];

and functionCode is always nil.Any idea what wrong?

Upvotes: 0

Views: 51

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Don't bother with regular expressions for this. If the format of the string is always the same then you can simply do:

NSString *dataStr2 = @"c&&(b.signature=Rk(c));return ql(a,b)}";
NSString *functionCode = [dataStr2 substringWithRange:NSMakeRange(16, 2)];

If the string is not quite so fixed then base it on the position of the =.

NSString *dataStr2 = @"c&&(b.signature=Rk(c));return ql(a,b)}";
NSRange equalRange = [dataStr2 rangeOfString:@"="];
NSString *functionCode = [dataStr2 substringWithRange:NSMakeRange(equalRange.location + equalRange.length, 2)];

Upvotes: 1

Related Questions