NSString *yourFuncionName(NSString *string) { NSString *regExString = @"yourRegularExpression"; NSRegularExpression *_regEx = [NSRegularExpression regularExpressionWithPattern:regExString options:NSRegularExpressionCaseInsensitive error:nil]; return [_regEx stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""]; } \\ s - removes all spaces.
[-: _ \\.] - removes all characters in square brackets
[: ^ digit:] - leaves only numbers
[: ^ alpha:] - leaves only letters
[: ^ alnum:] - leaves only letters and numbers
[: ^ word:] - leaves only letters, numbers and underscores
Important: do not forget to screen all meta-characters with "\\" in regular expressions
NSString *function_OnlyDigitsInString(NSString *string) { NSString *regExString = @"[:^digit:]"; NSRegularExpression *_regEx = [NSRegularExpression regularExpressionWithPattern:regExString options:NSRegularExpressionCaseInsensitive error:nil]; return [_regEx stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""]; } Source: https://habr.com/ru/post/279563/
All Articles