-1

I want to match number-number expression.I want to get input from textField, and need to format in way that,i can use that two numbers How can i do this, by using regular expression or something else. Please give me a suggestion. User input. Number-Number. I have to filter out. number, number as Integer. Also i have to check whether user entered anything wrong in textfield.

1 Answer 1

1

If you use NSRegularExpression,

NSString *userInputString = @"1990-2020";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+)-(\\d+)" options:0 error:nil];
NSArray *matches = [regex matchesInString:userInputString options:NSMatchingReportProgress range:NSMakeRange(0, userInputString.length)];

for (NSTextCheckingResult *match in matches)
{
    NSLog(@"%@", [userInputString substringWithRange:[match rangeAtIndex:1]]);
    NSLog(@"%@", [userInputString substringWithRange:[match rangeAtIndex:2]]);
}

output:
1990
2020

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.