Use Regexkitlite in iOS to try regular expressions

Source: Internet
Author: User

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.

  1. NSString *searchstring = @ "This is neat.";
  2. NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
  3. Nsrange Matchedrange = Nsmakerange (Nsnotfound, 0UL);
  4. Nserror *error = NULL;
  5. Matchedrange = [searchstring rangeofregex:regexstring options:rklnooptions inrange:searchrange Capture:2L error:& ERROR];
  6. NSLog (@"Matchedrange:%@", Nsstringfromrange (Matchedrange));
  7. 2008-03-18 03:51:16.530 test[51583:813] Matchedrange: {5, 2},//match to ' is '
  8. NSString *matchedstring = [SearchString substringwithrange:matchedrange];
  9. NSLog (@"matchedstring: '%@ '", matchedstring);
  10. 2008-03-18 03:51:16.532 test[51583:813] matchedstring: ' is '//generate substring
[CPP]View Plaincopy
  1. NSString *searchstring = @"This is neat.";
  2. NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
  3. Nsrange Matchedrange = Nsmakerange (Nsnotfound, 0UL);
  4. Nserror *error = NULL;
  5. Matchedrange = [searchstring rangeofregex:regexstring options:rklnooptions inrange:searchrange Capture:2L error:& ERROR];
  6. NSLog (@"Matchedrange:%@", Nsstringfromrange (Matchedrange));
  7. 2008-03-18 03:51:16.530 test[51583:813] Matchedrange: {5, 2},//match to ' is '
  8. NSString *matchedstring = [SearchString substringwithrange:matchedrange];
  9. NSLog (@"matchedstring: '%@ '", matchedstring);
  10. 2008-03-18 03:51:16.532 test[51583:813] matchedstring: ' is '//generate substring


2. Find the first match and return a nsstring
    1. NSString *searchstring = @ "This is neat.";
    2. NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
    3. NSString *matchedstring = [searchstring stringbymatching:regexstring capture:2l];
    4. NSLog (@"matchedstring: '%@ '", matchedstring);
    5. 2008-03-18 03:53:42.949 test[51583:813] matchedstring: ' Is '
[CPP]View Plaincopy
    1. NSString *searchstring = @"This is neat.";
    2. NSString *regexstring = @"(//w+)//s+ (//w+)//s+ (//w+)";
    3. NSString *matchedstring = [searchstring stringbymatching:regexstring capture:2l];
    4. NSLog (@"matchedstring: '%@ '", matchedstring);
    5. 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
  1. NSString *searchstring = @ "This is neat.";
  2. NSString *regexstring = @"//b (//w+)//b";
  3. NSString *replacewithstring = @"{$}";
  4. NSString *replacedstring = NULL;
  5. replacedstring = [searchstring stringbyreplacingoccurrencesofregex:regexstring withstring:replacewithstring];
  6. Nsmutablestring can be replaced directly and return the number of replacements
  7. NSLog (@"replaced string: '%@ '", replacedstring);
  8. 2008-07-01 19:03:03.195 test[68775:813] replaced string: ' {this} {is} {neat}. '
  9. nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@"This is neat."];
  10. NSString *regexstring = @"//b (//w+)//b";
  11. NSString *replacewithstring = @"{$}";
  12. Nsuinteger replacedcount = 0UL;
  13. Replacedcount = [mutablestring replaceoccurrencesofregex:regexstring withstring:replacewithstring];
  14. NSLog (@"Count:%lu string: '%@ '", (u_long) Replacedcount, mutablestring);
  15. 2008-07-01 21:25:43.433 test[69689:813] count:3 string: ' {this} {is} {neat}. '
[CPP]View Plaincopy
  1. NSString *searchstring = @"This is neat.";
  2. NSString *regexstring = @"//b (//w+)//b";
  3. NSString *replacewithstring = @"{$}";
  4. NSString *replacedstring = NULL;
  5. replacedstring = [searchstring stringbyreplacingoccurrencesofregex:regexstring withstring:replacewithstring];
  6. Nsmutablestring can be replaced directly and return the number of replacements
  7. NSLog (@"replaced string: '%@ '", replacedstring);
  8. 2008-07-01 19:03:03.195 test[68775:813] replaced string: ' {this} {is} {neat}. '
  9. nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@"This is neat."];
  10. NSString *regexstring = @"//b (//w+)//b";
  11. NSString *replacewithstring = @"{$}";
  12. Nsuinteger replacedcount = 0UL;
  13. Replacedcount = [mutablestring replaceoccurrencesofregex:regexstring withstring:replacewithstring];
  14. NSLog (@"Count:%lu string: '%@ '", (u_long) Replacedcount, mutablestring);
  15. 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
    1. NSString *searchstring = @ "This is neat.";
    2. NSString *regexstring = @"//s+";
    3. Nsarray *splitarray = NULL;
    4. Splitarray = [SearchString componentsseparatedbyregex:regexstring];
    5. Splitarray = = {@ ' this ', @ ' is ', @ ' neat. '}
    6. NSLog (@"Splitarray:%@", Splitarray);
[CPP]View Plaincopy
    1. NSString *searchstring = @"This is neat.";
    2. NSString *regexstring = @"//s+";
    3. Nsarray *splitarray = NULL;
    4. Splitarray = [SearchString componentsseparatedbyregex:regexstring];
    5. Splitarray = = {@ ' this ', @ ' is ', @ ' neat. '}
    6. NSLog (@"Splitarray:%@", Splitarray);


5. Returns all matching string arrays, although there are multiple parentheses in this example, but Componentsmatchedbyregex no matter
  1. NSString *searchstring = @ "$10.23, $1024.42, $3099";
  2. NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
  3. Nsarray *matcharray = NULL;
  4. Matcharray = [SearchString componentsmatchedbyregex:regexstring];
  5. Matcharray = = {@ "$10.23", @ "$1024.42", @ "$3099"};
  6. NSLog (@"Matcharray:%@", Matcharray);
  7. 6. Return all matching string arrays to handle all parentheses
  8. NSString *searchstring = @"$10.23, $1024.42, $3099";
  9. NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
  10. Nsarray *capturesarray = NULL;
  11. Capturesarray = [SearchString arrayofcapturecomponentsmatchedbyregex:regexstring];
  12. /* Capturesarray = =
  13. [Nsarray arraywithobjects:
  14. [Nsarray arraywithobjects: @ "$10.23", @ "10.23", @ "ten" @ "@", NULL],
  15. [Nsarray arraywithobjects:@ "$1024.42", @ "1024.42", @ "1024x768", @ "$", NULL],
  16. [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", NULL],
  17. NULL];
  18. */
  19. NSLog (@"Capturesarray:%@", Capturesarray);
  20. Output Result:
  21. shell%./capturesarray↵
  22. 2009-05-06 03:25:46.852 capturesarray[69981:10b] Capturesarray: (
  23. (
  24. "$10.23",
  25. "10.23",
  26. 10,
  27. 23
  28. ),
  29. (
  30. "$1024.42",
  31. "1024.42",
  32. 1024,
  33. 42
  34. ),
  35. (
  36. "$3099",
  37. 3099,
  38. 3099,
  39. ""   
  40. )
  41. )
[CPP]View Plaincopy
  1. NSString *searchstring = @"$10.23, $1024.42, $3099";
  2. NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
  3. Nsarray *matcharray = NULL;
  4. Matcharray = [SearchString componentsmatchedbyregex:regexstring];
  5. Matcharray = = {@ "$10.23", @ "$1024.42", @ "$3099"};
  6. NSLog (@"Matcharray:%@", Matcharray);
  7. 6. Return all matching string arrays to handle all parentheses
  8. NSString *searchstring = @"$10.23, $1024.42, $3099";
  9. NSString *regexstring = @"//$ ((//d+) (?:/ /. (//d+) |//) ";
  10. Nsarray *capturesarray = NULL;
  11. Capturesarray = [SearchString arrayofcapturecomponentsmatchedbyregex:regexstring];
  12. /* Capturesarray = =
  13. [Nsarray arraywithobjects:
  14. [Nsarray arraywithobjects: @ "$10.23", @ "10.23", @ "ten" @ "@", NULL],
  15. [Nsarray arraywithobjects:@ "$1024.42", @ "1024.42", @ "1024x768", @ "$", NULL],
  16. [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", NULL],
  17. NULL];
  18. */
  19. NSLog (@"Capturesarray:%@", Capturesarray);
  20. Output Result:
  21. shell%./capturesarray↵
  22. 2009-05-06 03:25:46.852 capturesarray[69981:10b] Capturesarray: (
  23. (
  24. "$10.23",
  25. "10.23",
  26. 10,
  27. 23
  28. ),
  29. (
  30. "$1024.42",
  31. "1024.42",
  32. 1024,
  33. 42
  34. ),
  35. (
  36. "$3099",
  37. 3099,
  38. 3099,
  39. ""  
  40. )
  41. )

Use Regexkitlite in iOS to try regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.