Use regex: RegexKitLite .
This is a “complete example” of how to use regex to do what you want with a lot of explanation, so this is a bit long answer. The regex used is just one way to do this and is "fairly permissive" in what it accepts. An example shows:
- How to combine more than one line / directory at once.
- Possible way to handle different date formats (
Jan 10 05:30 and Apr 30 2009 ) - How to create an "array of arrays" of matches.
- Iterate over the mapped array and create an
NSDictionary based on the parsed results. - Create a version of the results with commas.
Note. . The example splits some of its long lines into several lines. A string literal in the form @"string1 " @"string2" will be "automatically" concatenated by the compiler to form a string equivalent to @"string 1 string2" . I only note this because it may seem a little unusual if you are not used to it.
#import <Foundation/Foundation.h>
Compile and run with:
shell% gcc -Wall -Wmost -arch i386 -g -o regexExample regexExample.m RegexKitLite.m -framework Foundation -licucore shell% ./regexExample 2010-01-14 00:10:38.868 regexExample[49409:903] allMatchesArray: ( ( "drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California", "drwxr-xr-x", 9, 0, 0, 4096, "Jan 10 05:30", California ), ( "-rw-r--r-- 1 johne staff 1335 Apr 30 2009 tags.m", "-rw-r--r--", 1, johne, staff, 1335, "Apr 30 2009", "tags.m" ) ) 2010-01-14 00:10:38.872 regexExample[49409:903] parsedDictionary: { date = "Jan 10 05:30"; group = 0; links = 9; name = California; permission = "drwxr-xr-x"; size = 4096; user = 0; } 2010-01-14 00:10:38.873 regexExample[49409:903] parsedDictionary: { date = "Apr 30 2009"; group = staff; links = 1; name = "tags.m"; permission = "-rw-r--r--"; size = 1335; user = johne; } 2010-01-14 00:10:38.873 regexExample[49409:903] commaSeparatedString: drwxr-xr-x,9,0,0,4096,Jan 10 05:30,California -rw-r--r--,1,johne,staff,1335,Apr 30 2009,tags.m
source share