Regular Expressions are often used when processing strings, which are no exception on iphone OS. Use RegexKit Frameworkhttp: // regexkit.sourceforge.net/RegexKitLite/index.html. Here the http://downloads.sourceforge.net/regexkit/RegexKitLite-4.0.tar.bz2 downloads RegexKitLite.
Unzip the RegexKitLite-4.0.tar.bz2:
1. RegexKitLite. h
2. RegexKitLite. m
3. RegexKitLite.html
4. examples
5. RKLMatchEnumerator. h
6. RKLMatchEnumerator. m
7. NSString-HexConversion.h
NSString-HexConversion.m
9. link_example.m
10. main. m
Here, we only need to add the RegexKitLite. h and RegexKitLite. m files to your project. In addition, the-licucore link switch is added. A simple example is as follows:
1. NSString * searchString = @ "This is neat .";
2. NSString * regexString = @ "\ B (\ w +) \ B ";
3. NSString * replaceWithString = @ "{$1 }";
4. NSString * replacedString = NULL;
5.
6. replacedString = [searchString stringByReplacingOccurrencesOfRegex: regexString withString: replaceWithString];
7. NSLog (@ "replaced string: '% @'", replacedString );
The output result is:
1. replaced string: '{This} {is} {neat }.'
You can also use Enumerator to obtain each matching item.
1. # import <Foundation/ngutoreleasepool. h>
2. # import "RegexKitLite. h"
3. # import "RKLMatchEnumerator. h"
4.
5. int main (int argc, char * argv []) {
6. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
7.
8. NSString * searchString = @ "one \ ntwo \ n \ nfour \ n ";
9. NSEnumerator * matchEnumerator = NULL;
10. NSString * regexString = @"(? M) ^. * [DISCUZ_CODE_3] quot ;;
11.
12. NSLog (@ "searchString: '% @'", searchString );
13. NSLog (@ "regexString: '% @'", regexString );
14.
15. matchEnumerator = [searchString matchEnumeratorWithRegex: regexString];
16.
17. NSUInteger line = 0;
18. NSString * matchedString = NULL;
19.
20. while (matchedString = [matchEnumerator nextObject])! = NULL ){
21. NSLog (@ "% d: % d' % @ '", ++ line, [matchedString length], matchedString );
22 .}
23.
24. [pool release];
25. return (0 );
26 .}
The following is an example of how to match a string in HTML. Extract the value of the alt attribute from the img-tag.
1.
2.
1. NSString * details = [item objectForKey: @ "description"];
2. if ([details length]> 0 ){
3. NSString * searchString = [details stringByHalfwideningLatinCharacters];
4.
5. NSEnumerator * matchEnumerator = NULL;
6. NSString * regex = @ "] + alt = \" ([^>] +) \ "[^>] *> ";
7. matchEnumerator = [searchString matchEnumeratorWithRegex: regex];
8. NSUInteger line = 0;
9. NSString * matchedString = NULL;
10. while (matchedString = [matchEnumerator nextObject])! = NULL ){
11. NSString * imgTag = matchedString;
12. NSMutableString * alt = [NSMutableString stringWithString: imgTag];
13.
14. NSString * replaceWithString = @ "$1 ";
15. NSUInteger replacedCount = [alt replaceOccurrencesOfRegex: regex withString: replaceWithString];
16. if (replacedCount ){
17. NSString * abbr = [abbreviationMappings objectForKey: alt];
18. if (! Abbr ){
19. abbr = [NSString stringWithFormat: @ "[% @]", alt];
20 .}
21. searchString = [searchString stringByReplacingOccurrencesOfString: imgTag withString: abbr];
22 .}
23. line ++;
24 .}
25. program. details = searchString;
26 .}
Convert string
1. NSString * result;
2. NSString * sample = @ "Phone Num: 010-123-456-789 ";
3. NSString * regex = @ "(\ d {3 })-";
4. NSString * replace = @ "$1 ,";
5.
6. result = [sample stringByReplacingOccurrencesOfRegex: regex withString: replace];
7. NSLog (@ "replace: % @", result );
In the example shown above, "-" between numbers is replaced with "," and the output result is:
1. replace: telephone Num: 010,123,456,789
Split string
1. NSString * sample = @ "This is sample ";
2. NSString * regex = @ "\ s + ";
3. NSArray * results = [sample componentsSeparatedByRegex: regex];
4. NSLog (@ "results: % @", results );
The result is as follows:
1. results :(
2. This,
3. is,
4. sample
5 .)
In addition, there are many practical places for you to continue the study if you are interested.
From ioser