ext.: http://blog.csdn.net/nullcn/article/details/6338592
Ready to work, download the Regexkitlite software package, unzip it with 2 files that need to be loaded into project.
And then load the framework libicucore.dylib, because Regexkitlite is calling the API inside, Apple rules out the use of proprietary APIs and non-published APIs. In fact, Regexkitlite to NSString extension, currently only support NSString, for me also enough ...
Examples of basic use (see official documentation for more information)
1.
- NSString *searchstring = @ "This is neat.";
- NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
- Nsrange Matchedrange = Nsmakerange (Nsnotfound, 0UL);
- Nserror *error = NULL;
- Matchedrange = [searchstring rangeofregex:regexstring options:rklnooptions inrange:searchrange Capture:2L error:& ERROR];
- NSLog (@"Matchedrange:%@", Nsstringfromrange (Matchedrange));
- 2008-03-18 03:51:16.530 test[51583:813] Matchedrange: {5, 2},//match to ' is '
- NSString *matchedstring = [SearchString substringwithrange:matchedrange];
- NSLog (@"matchedstring: '%@ '", matchedstring);
- 2008-03-18 03:51:16.532 test[51583:813] matchedstring: ' is '//generate substring
[CPP]View Plaincopy
- NSString *searchstring = @"This is neat.";
- NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
- Nsrange Matchedrange = Nsmakerange (Nsnotfound, 0UL);
- Nserror *error = NULL;
- Matchedrange = [searchstring rangeofregex:regexstring options:rklnooptions inrange:searchrange Capture:2L error:& ERROR];
- NSLog (@"Matchedrange:%@", Nsstringfromrange (Matchedrange));
- 2008-03-18 03:51:16.530 test[51583:813] Matchedrange: {5, 2},//match to ' is '
- NSString *matchedstring = [SearchString substringwithrange:matchedrange];
- NSLog (@"matchedstring: '%@ '", matchedstring);
- 2008-03-18 03:51:16.532 test[51583:813] matchedstring: ' is '//generate substring
2. Find the first match and return a nsstring
- NSString *searchstring = @ "This is neat.";
- NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
- NSString *matchedstring = [searchstring stringbymatching:regexstring capture:2l];
- NSLog (@"matchedstring: '%@ '", matchedstring);
- 2008-03-18 03:53:42.949 test[51583:813] matchedstring: ' Is '
[CPP]View Plaincopy
- NSString *searchstring = @"This is neat.";
- NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
- NSString *matchedstring = [searchstring stringbymatching:regexstring capture:2l];
- NSLog (@"matchedstring: '%@ '", matchedstring);
- 2008-03-18 03:53:42.949 test[51583:813] matchedstring: ' Is '
3. Find and replace, parentheses and concepts are the same as in Python, meaning the contents of the first parenthesis
- NSString *searchstring = @ "This is neat.";
- NSString *regexstring = @"//b (//w+)//b";
- NSString *replacewithstring = @"{$}";
- NSString *replacedstring = NULL;
- replacedstring = [searchstring stringbyreplacingoccurrencesofregex:regexstring withstring:replacewithstring];
- Nsmutablestring can be replaced directly and return the number of replacements
- NSLog (@"replaced string: '%@ '", replacedstring);
- 2008-07-01 19:03:03.195 test[68775:813] replaced string: ' {this} {is} {neat}. '
- nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@"This is neat."];
- NSString *regexstring = @"//b (//w+)//b";
- NSString *replacewithstring = @"{$}";
- Nsuinteger replacedcount = 0UL;
- Replacedcount = [mutablestring replaceoccurrencesofregex:regexstring withstring:replacewithstring];
- NSLog (@"Count:%lu string: '%@ '", (u_long) Replacedcount, mutablestring);
- 2008-07-01 21:25:43.433 test[69689:813] count:3 string: ' {this} {is} {neat}. '
[CPP]View Plaincopy
- NSString *searchstring = @"This is neat.";
- NSString *regexstring = @"//b (//w+)//b";
- NSString *replacewithstring = @"{$}";
- NSString *replacedstring = NULL;
- replacedstring = [searchstring stringbyreplacingoccurrencesofregex:regexstring withstring:replacewithstring];
- Nsmutablestring can be replaced directly and return the number of replacements
- NSLog (@"replaced string: '%@ '", replacedstring);
- 2008-07-01 19:03:03.195 test[68775:813] replaced string: ' {this} {is} {neat}. '
- nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@"This is neat."];
- NSString *regexstring = @"//b (//w+)//b";
- NSString *replacewithstring = @"{$}";
- Nsuinteger replacedcount = 0UL;
- Replacedcount = [mutablestring replaceoccurrencesofregex:regexstring withstring:replacewithstring];
- NSLog (@"Count:%lu string: '%@ '", (u_long) Replacedcount, mutablestring);
- 2008-07-01 21:25:43.433 test[69689:813] count:3 string: ' {this} {is} {neat}. '
4. For splitting, returns a split array of strings
- NSString *searchstring = @ "This is neat.";
- NSString *regexstring = @"//s+";
- Nsarray *splitarray = NULL;
- Splitarray = [SearchString componentsseparatedbyregex:regexstring];
- Splitarray = = {@ ' this ', @ ' is ', @ ' neat. '}
- NSLog (@"Splitarray:%@", Splitarray);
[CPP]View Plaincopy
- NSString *searchstring = @"This is neat.";
- NSString *regexstring = @"//s+";
- Nsarray *splitarray = NULL;
- Splitarray = [SearchString componentsseparatedbyregex:regexstring];
- Splitarray = = {@ ' this ', @ ' is ', @ ' neat. '}
- NSLog (@"Splitarray:%@", Splitarray);
5. Returns all matching string arrays, although there are multiple parentheses in this example, but Componentsmatchedbyregex no matter
- NSString *searchstring = @ "$10.23, $1024.42, $3099";
- NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
- Nsarray *matcharray = NULL;
- Matcharray = [SearchString componentsmatchedbyregex:regexstring];
- Matcharray = = {@ "$10.23", @ "$1024.42", @ "$3099"};
- NSLog (@"Matcharray:%@", Matcharray);
- 6. Return all matching string arrays to handle all parentheses
- NSString *searchstring = @"$10.23, $1024.42, $3099";
- NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
- Nsarray *capturesarray = NULL;
- Capturesarray = [SearchString arrayofcapturecomponentsmatchedbyregex:regexstring];
- /* Capturesarray = =
- [Nsarray arraywithobjects:
- [Nsarray arraywithobjects: @ "$10.23", @ "10.23", @ "ten" @ "@", NULL],
- [Nsarray arraywithobjects:@ "$1024.42", @ "1024.42", @ "1024x768", @ "$", NULL],
- [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", NULL],
- NULL];
- */
- NSLog (@"Capturesarray:%@", Capturesarray);
- Output Result:
- shell%./capturesarray↵
- 2009-05-06 03:25:46.852 capturesarray[69981:10b] Capturesarray: (
- (
- "$10.23",
- "10.23",
- 10,
- 23
- ),
- (
- "$1024.42",
- "1024.42",
- 1024,
- 42
- ),
- (
- "$3099",
- 3099,
- 3099,
- ""
- )
- )
[CPP]View Plaincopy
- NSString *searchstring = @"$10.23, $1024.42, $3099";
- NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
- Nsarray *matcharray = NULL;
- Matcharray = [SearchString componentsmatchedbyregex:regexstring];
- Matcharray = = {@ "$10.23", @ "$1024.42", @ "$3099"};
- NSLog (@"Matcharray:%@", Matcharray);
- 6. Return all matching string arrays to handle all parentheses
- NSString *searchstring = @"$10.23, $1024.42, $3099";
- NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
- Nsarray *capturesarray = NULL;
- Capturesarray = [SearchString arrayofcapturecomponentsmatchedbyregex:regexstring];
- /* Capturesarray = =
- [Nsarray arraywithobjects:
- [Nsarray arraywithobjects: @ "$10.23", @ "10.23", @ "ten" @ "@", NULL],
- [Nsarray arraywithobjects:@ "$1024.42", @ "1024.42", @ "1024x768", @ "$", NULL],
- [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", NULL],
- NULL];
- */
- NSLog (@"Capturesarray:%@", Capturesarray);
- Output Result:
- shell%./capturesarray↵
- 2009-05-06 03:25:46.852 capturesarray[69981:10b] Capturesarray: (
- (
- "$10.23",
- "10.23",
- 10,
- 23
- ),
- (
- "$1024.42",
- "1024.42",
- 1024,
- 42
- ),
- (
- "$3099",
- 3099,
- 3099,
- ""
- )
- )
Use Regexkitlite in iOS to try regular expressions