I have a string of format NSString *CalculationFormula = @”((Col(202)/Col(201)-1)*100″;
I need to replace all occurrences of Col(number) with ABC(number * 10)
Please help …
Thanks,
Ben
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s some ambiguity in the desired results. For example, should the result be “ABC(202 * 10)” or “ABC(2020)”? I’m going to assume the latter. Since regex is fairly generic, here’s a Perl snippet that accomplishes what I think you want, followed by its translation into Cocoa. I’ve given both, because it’s much easier to see what’s going on there before moving onto
NSRegularExpressionbecause the latter has so many more escapes in the pattern.Perl version:
Prints:
((ABC(2020)/ABC(2010)-1)*100So, the matching pattern is
Col\((?P<num>\d+)\)and the substitution patternABC($+{num}0Cocoa version:
Logs:
Now, if my original assumption was wrong and you literally want “num * 10” in the result,then the substitution pattern is: