I had to do this recently and wanted to use an effective method:
(assuming someText is an NSString or text attribute)
NSString* someText = @"1232342jfahadfskdasfkjvas12!";
(in this example, lines from line will be broken)
[someText stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [someText length])];
Keep in mind that you need to escape the regex character using the Obj-c escape character:
(obj-c uses double backslashes to avoid special regular expressions)
...stringByReplacingOccurrencesOfString:@"[\\\!\\.:\\/]"
What makes it interesting is that the NSRegularExpressionSearch option is not used much, but can lead to some very powerful controls:
You can find the iOS regex tutorial here and more on regex at regex101.com
Tommie C. Sep 06 '14 at 10:15 2014-09-06 22:15
source share