I want to be able to find a substring within a sting but it has a distinctive pattern I am not sure how to find.
EX.
NSString *test1= @"Contact Names
67-444-322
Dec 21 2012
23941 6745 9145072 01567
5511 23345 614567 123456
Older Contacts
See Back Side";
I want to find the following pattern within the substring (these numbers but not date numbers)
23941 6745 9145072 01567
5511 23345 614567 123456
However, the format of the example string will hardly ever be the same. There will be different numbers and a different title every time other than “Contact Names”, “Older Contacts” and “See Back Side”. One thing that will remain constant is that the numbers I am looking for will always have 4 numbers but there could be 1 row or 10 rows.
Does anyone know how I would go about this problem? I was thinking something in terms of perhaps finding only the numbers within the string and then checking to see which numbers have 3 spaces in between.
Thanks
You could use character sets to separate the string and then determine if there are 4 numbers in each component. This will only work though if the string has newline characters (
\n) in it (as your response to Lance seems to indicate).This is how I would go about it:
Sorry about the long code lines, some of Apple’s selectors are a little on the long side… In any case, if elements has 4 individual (
NSString) objects, then it is one of the lines you are seeking and you can manipulate the data as you need.EDIT (aside):
On the topic of Regex (as this question contains the
regextag), yes you could use regular expressions, but Objective-C doesn’t really have a ‘nice’ way of handling them… Regex is more in the domain of scripting languages and languages which have built-in support for it.