RegExKitLite Expression Question

RegExKitLite Expression Question

I'm having trouble coming up with an RegExKitLite expression that will match. I'm parsing a string and I want it to grab everything till it comes upon the first occurrence of a colon

What would be the expression in RegExKitLite to do that?


Address Book picker memory usage mystery in iPhone 3.0 project


How to dismiss UITableView?
This regex will match everything from the start until (but excluding) the first colon:.
Reading letters from a custom iPhone keyboard

To include the first colon is as simple as putting it on the end:.
Returning an array in cocoa, but waiting for delegation to complete

So, to use either of those with RegexKitLite, you can do:.
How to select a UITextField?
NSString * firstItem = [someString stringByMatching:@"^[^:]*" capture:0]; 
Note how there is no parentheses - since * is greedy you can simply use the negated class and use captured group 0 (i.e.

How does hiding a UIStatusBar apply to UIViews being shown?
the whole match).

Quartz caching CGLayer
Presenting a modal view controller only after another one has been dismissed

It's worth noting that most languages will include functions that allow you to do this with a regular function, for example ListFirst(MyString,':') or MyString.split(':')[0]. I suspect Objective-C has something similar to this ...

yep, see here.
NSString *string = @"oop:ack:bork:greeble:ponies"; NSArray *chunks = [string componentsSeparatedByString: @":"]; 


To do this specifically with RegexKitLite, you'll need to do the following:. Add the RegexKitLite.h/.m files to your project. Import RegexKitLite.h into the file where you need to use regular expressions. Use the following to grab the stuff before the colon:.
NSString * everythingBeforeTheColon = [someString stringByMatching:@"([^:]*):" capture:1]; 


I just updated my SO answer here, so I figured I'd use that to benchmark the standard foundation componentsSeparatedByString: and RegexKitLites componentsSeparatedByRegex:.

The line of code inside the for() loop for each was (essentially):.
NSString *string = @"oop:ack:bork:greeble:ponies"; for() { NSArray *chunks = [string componentsSeparatedByString: @":"]; } for() { NSArray *chunks = [string componentsSeparatedByRegex: @":"]; } 
Times returned were (time is in microseconds per operation):.
componentsSeparatedByString: 3.96810us componentsSeparatedByRegex:  2.46155us 
EDIT:. I thought I'd go one better: How to use RegexKitLite to create a NSArray of NSArrays from a string containing multiple lines of colon separated data (ie, /etc/passwd).

Modified from the comma separated value example in the RegexKitLite documentation.

When finished, the variable splitLinesArray contains the finished product..
NSString   *theString  = @"a:b:c\n1:2:3\nX:Y:Z\n"; // An example string to work on. 

NSArray *linesArray = [theString componentsSeparatedByRegex:@"(?:\r\n|[\n\v\f\r\\x85\\p{Zl}\\p{Zp}])"]; id splitLines[[linesArray count]]; NSUInteger splitLinesIndex = 0UL; for(NSString *lineString in linesArray) { splitLines[splitLinesIndex++] = [lineString componentsSeparatedByRegex:@":"]; } NSArray *splitLinesArray = [NSArray arrayWithObjects:splitLines count:splitLinesIndex];

98 out of 100 based on 48 user ratings 398 reviews