Use regexkitlite in IOS to try Regular Expressions

Source: Internet
Author: User
Use regexkitlite in IOS to try Regular Expressions
Favorites

Preparations: Download regexkitlite
Package. After decompression, there are two files that need to be loaded into the project.

And then load the framework.
Libicucore. dylib
Because regexkitlite calls the APIs in this interface, Apple stipulates that private APIs and unpublished APIs cannot be used. In fact, regexkitlite extends nsstring. Currently, only nsstring is supported, which is enough for me...

Basic examples (for more information, see
Official documentation

)

1.

  1. Nsstring * searchstring = @
    "This is neat ."
    ;
  2. Nsstring * regexstring = @ "(// W +) // s + (// W + )"
    ;
  3. Nsange 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. // 03:51:16. 530 test [51583: 813] matchedrange: {5, 2}, // match to 'is'

  8. Nsstring * matchedstring = [searchstring substringwithrange: matchedrange];
  9. Nslog (@ "matchedstring: '% @'"
    , Matchedstring );
  10. // 03:51:16. 532 test [51583: 813] matchedstring: 'is' // generate a substring

Nsstring * searchstring = @ "This is neat. "; <br/> nsstring * regexstring = @" (// W +) // s + (// W + )"; <br/> nsange matchedrange = nsmakerange (nsnotfound, 0ul); <br/> nserror * error = NULL; <br/> matchedrange = [searchstring rangeofregex: regexstring options: rklnooptions inrange: searchrange Capture: 2L error: & error]; <br/> nslog (@ "matchedrange: % @", nsstringfromrange (matchedrange); <br/> // 03:51:16. 530 test [51583: 813] matchedrange: {5, 2}, // match to 'is '<br/> nsstring * matchedstring = [searchstring substringwithrange: matchedrange]; <br/> nslog (@ "matchedstring: '% @'", matchedstring); <br/> // 2008-03-18 03:51:16. 532 test [51583: 813] matchedstring: 'Is // generate a substring

2. Find the first match and return an nsstring

  1. Nsstring * searchstring = @
    "This is neat ."
    ;
  2. Nsstring * regexstring = @ "(// W +) // s + (// W + )"
    ;
  3. Nsstring * matchedstring = [searchstring stringbymatching: regexstring Capture: 2L];
  4. Nslog (@ "matchedstring: '% @'"
    , Matchedstring );
  5. // 03:53:42. 949 test [51583: 813] matchedstring: 'Is'

Nsstring * searchstring = @ "This is neat. "; <br/> nsstring * regexstring = @" (// W +) // s + (// W + )"; <br/> nsstring * matchedstring = [searchstring stringbymatching: regexstring Capture: 2L]; <br/> nslog (@ "matchedstring: '% @'", matchedstring ); <br/> // 03:53:42. 949 test [51583: 813] matchedstring: 'Is'

3. search and replace. The brackets and concepts are the same as those in Python. $1 indicates the content in the first bracket.

  1. Nsstring * searchstring = @
    "This is neat ."
    ;
  2. Nsstring * regexstring = @ "// B (// W +) // B"
    ;
  3. Nsstring * replacewithstring = @ "{$1 }"
    ;
  4. Nsstring * replacedstring = NULL;
  5. Replacedstring = [searchstring stringbyreplacingoccurrencesofregex: regexstring withstring: replacewithstring];
  6. // Nsmutablestring can be replaced directly, and the number of replacement times is returned.

  7. Nslog (@ "replaced string: '% @'"
    , Replacedstring );
  8. // 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 = @ "{$1 }"
    ;
  12. Nsuinteger replacedcount = 0ul;
  13. Replacedcount = [mutablestring replaceoccurrencesofregex: regexstring withstring: replacewithstring];
  14. Nslog (@ "count: % lu string: '% @'"
    , (U_long) replacedcount, mutablestring );
  15. // 21:25:43. 433 test [69689: 813] Count: 3 string: '{This} {is} {neat }.'

Nsstring * searchstring = @ "This is neat. "; <br/> nsstring * regexstring = @" // B (// W +) // B "; <br/> nsstring * replacewithstring = @ "{$1}"; <br/> nsstring * replacedstring = NULL; <br/> replacedstring = [searchstring success: regexstring withstring: replacewithstring]; <br/> // nsmutablestring can be replaced directly, and the number of replacement times are returned <br/> nslog (@ "replaced string: '% @'", replacedstring ); <br/> // 19:03:03. 195 test [68775: 813] Replaced string: '{This} {is} {neat }. '<br/> nsmutablestring * mutablestring = [nsmutablestring stringwithstring: @ "This is neat. "]; <br/> nsstring * regexstring = @" // B (// W +) // B "; <br/> nsstring * replacewithstring = @ "{$1}"; <br/> nsuinteger replacedcount = 0ul; <br/> replacedcount = [mutablestring replaceoccurrencesofregex: regexstring withstring: replacewithstring]; <br/> nslog (@ "count: % lu string: '% @'", (u_long) replacedcount, mutablestring); <br/> // 2008-07-01 21:25:43. 433 test [69689: 813] Count: 3 string: '{This} {is} {neat }.'

4. Used for splitting. A string array after splitting is returned.

  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 );

Nsstring * searchstring = @ "This is neat. "; <br/> nsstring * regexstring = @"/S + "; <br/> nsarray * splitarray = NULL; <br/> splitarray = [searchstring componentsseparatedbyregex: regexstring]; <br/> // splitarray ={@ "this", @ "is", @ "neat. "} <br/> nslog (@" splitarray: % @ ", splitarray );

5. Return all matching string arrays. In this example, although there are multiple parentheses
Componentsmatchedbyregex

  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 process 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", @ "10", @ "23", null],
     
  15. [Nsarray arraywithobjects: @ "$1024.42", @ "1024.42", @ "1024", @ "42", null],
     
  16. [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", null],
     
  17. Null];
     
  18. */

  19. Nslog (@ "capturesarray: % @"
    , Capturesarray );
  20. Output result:
  21. Shell %./capturesarray restart
  22. 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. )

Nsstring * searchstring = @ "$10.23, $1024.42, $3099"; <br/> nsstring * regexstring = @ "// $ (// D + )(?: //. (// D +) | //.?)) "; <Br/> nsarray * matcharray = NULL; <br/> matcharray = [searchstring componentsmatchedbyregex: regexstring]; <br/> // matcharray =={@ "$10.23", @ "$1024.42", @ "$3099" }; <br/> nslog (@ "matcharray: % @ ", matcharray); <br/> 6. returns all matching string arrays to process all parentheses <br/> nsstring * searchstring = @ "$10.23, $1024.42, $3099 "; <br/> nsstring * regexstring = @ "// $ (// D + )(?: //. (// D +) | //.?)) "; <Br/> nsarray * capturesarray = NULL; <br/> capturesarray = [searchstring arrayofcapturecomponentsmatchedbyregex: regexstring]; <br/>/* capturesarray = <br/> [nsarray arraywithobjects: <br/> [nsarray arraywithobjects: @ "$10.23", @ "10.23 ", @ "10", @ "23", null], <br/> [nsarray arraywithobjects: @ "$1024.42", @ "1024.42", @ "1024 ", @ "42", null], <br/> [nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", null], <br/> null]; <br/> */<br/> nslog (@ "capturesarray: % @", capturesarray); <br/> output result: <br/> shell %. /capturesarray failed <br/> 03:25:46. 852 capturesarray [69981: 10b] capturesarray: (<br/> "$10.23", <br/> "10.23", <br/> 10, <br/> 23 <br/>), <br/> (<br/> "$1024.42", <br/> "1024.42", <br/> 1024, <br/> 42 <br/>), <br/> (<br/> "$3099", <br/> 3099, <br/> 3099, <br/> "" <br/>)

Related Article

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.