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.
- Nsstring * searchstring = @
"This is neat ."
;
- Nsstring * regexstring = @ "(// W +) // s + (// W + )"
;
- Nsange matchedrange = nsmakerange (nsnotfound, 0ul );
- Nserror * error = NULL;
- Matchedrange = [searchstring rangeofregex: regexstring options: rklnooptions inrange: searchrange Capture: 2L error: & error];
- Nslog (@ "matchedrange: % @"
, Nsstringfromrange (matchedrange ));
- // 03:51:16. 530 test [51583: 813] matchedrange: {5, 2}, // match to 'is'
- Nsstring * matchedstring = [searchstring substringwithrange: matchedrange];
- Nslog (@ "matchedstring: '% @'"
, Matchedstring );
- // 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
- Nsstring * searchstring = @
"This is neat ."
;
- Nsstring * regexstring = @ "(// W +) // s + (// W + )"
;
- Nsstring * matchedstring = [searchstring stringbymatching: regexstring Capture: 2L];
- Nslog (@ "matchedstring: '% @'"
, Matchedstring );
- // 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.
- Nsstring * searchstring = @
"This is neat ."
;
- Nsstring * regexstring = @ "// B (// W +) // B"
;
- Nsstring * replacewithstring = @ "{$1 }"
;
- Nsstring * replacedstring = NULL;
- Replacedstring = [searchstring stringbyreplacingoccurrencesofregex: regexstring withstring: replacewithstring];
- // Nsmutablestring can be replaced directly, and the number of replacement times is returned.
- Nslog (@ "replaced string: '% @'"
, Replacedstring );
- // 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 = @ "{$1 }"
;
- Nsuinteger replacedcount = 0ul;
- Replacedcount = [mutablestring replaceoccurrencesofregex: regexstring withstring: replacewithstring];
- Nslog (@ "count: % lu string: '% @'"
, (U_long) replacedcount, mutablestring );
- // 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.
- Nsstring * searchstring = @
"This is neat ."
;
- Nsstring * regexstring = @ "// s +"
;
- Nsarray * splitarray = NULL;
- Splitarray = [searchstring componentsseparatedbyregex: regexstring];
- // Splitarray ={@ "this", @ "is", @ "neat ."}
- 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
- 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 process 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", @ "10", @ "23", null],
- [Nsarray arraywithobjects: @ "$1024.42", @ "1024.42", @ "1024", @ "42", null],
- [Nsarray arraywithobjects: @ "$3099", @ "3099", @ "3099", @ "", null],
- Null];
- */
- Nslog (@ "capturesarray: % @"
, Capturesarray );
- Output result:
- Shell %./capturesarray restart
- 03:25:46. 852 capturesarray [69981: 10b] capturesarray :(
- (
- "$10.23"
,
- "10.23"
,
- 10,
- 23
- ),
- (
- "$1024.42"
,
- "1024.42"
,
- 1024,
- 42
- ),
- (
- "$3099"
,
- 3099,
- 3099,
- ""
- )
- )
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/>)