Use regular expression-ogrekit for details on iPhone

Source: Internet
Author: User
Regular Expressions are very commonly used programming tools. In the past, regular expressions were used very frequently in Ruby and C. after learning cocoa, I have been wondering how to use regular expressions. I have been searching for a long time in the library that comes with cocoa and found that there is no corresponding support library (BS, this kind of common stuff doesn't exist, and I don't know what Apple thinks ....).  
I found a lot of libraries on the Internet, and used them either too large or uncomfortable in structure design. Finally, I locked the target on ogrekit.  

Yes, this is a JapaneseProgramWhat the staff wrote (people with no difference can go away), the biggest headache for me is that there is no English document .... EvenCodeThe annotations are in Japanese. Fortunately, the naming method is very reasonable. You can guess them by guessing. 
The following describes the main classes: 
Ogregularexpression (Regular Expression implementation) 
Ogregularexpressionenumerator (enumerator that matches the result set) 
Ogregularexpressionmatch (matching result) 
Ogreplaceexpression (replacement function implementation) 
Ogregularexpressionformatter (Regular Expression formatter) 

First, we will introduce ogregularexpression. 
By viewing its source file, you can see that the signature of the most parameter static constructor is as follows: 

+ (ID) regularexpressionwithstring :( nsstring *) expressionstring 
Options :( unsigned) Options 
Syntax :( ogresyntax) syntax 
Escapecharacter :( nsstring *) character; 

The options parameter can be a combination of the following values: 

ogrenoneoption has no options
ogresinglelineoption only matches a single row
ogremultilineoption '. 'line
ogreignorecaseoption case-insensitive
ogreextendoption uses extended matching mode
ogrefindlongestoption adopts the maximum matching mode
ogrefindnotemptyoption ignores NULL matching
ogrenegatesinglelineoption
ogredontcapturegroupoption only matches the group with a name
ogrecapturegroupoption matches the group with a name or no name
ogredelimitbywhitespace span>
the syntax parameter can be a combination of the following values:

Ogreposixbasicsyntax POSIX basic re 
Ogreposixextendedsyntax POSIX extended re 
Ogreemacssyntax Emacs 
Ogregrepsyntax grep 
Ogregnuregexsyntax GNU RegEx 
Ogresponyntax Java (Sun Java. util. RegEx) 
Ogreperlsyntax Perl 
Ogrerubysyntax Ruby (default) 
Ogresimplematchingsyntax Simple Matching 

Escapecharacter: 
Ogrebackslashcharacter @ "\" backslash (default) 
Ogreguiyencharacter [nsstring stringwithuf8string: "\ xc2 \ xa5"] yen mark 

It can be seen that this library has basically encapsulated all the functions of regular expressions, and is also compatible with multiple syntaxes and is very strong! 
Because many third-party libraries do not have any documentation (there are few fully-documented third-party Libraries under MAC), it is basically necessary to view the source files to understand the usage. 

Next let's take a look at how to use this library. 
1. The simplest example. 

// Construct a regular expression 
Ogregularexpression * RegEx = [ogregularexpression regularexpressionwithstring: @ "A [^ A] * A"]; 
// Construct the matching result enumerator 
Nsenumerator * enumerator = [RegEx matchenumeratorinstring: @ "alphabetagammadelta"]; 
Ogregularexpressionmatch * match; // defines the matching result. 
While (match = [enumerator nextobject])! = Nil) {// obtain the matching result string in sequence 
Nslog (@ "% @", [Match matchedstring]); 

// Matching result: 
// Alpha 
// Aga 
// Adelta 

2. Examples of using the replacement function. 

-(void) replacetest
{
ogregularexpression * celciusregex;
celciusregex = [ogregularexpression regularexpressionwithstring: @ "([+-]? \ D + (?: \. \ D + )?) C \ B "];
nsstring * targetstring = @" 36.5c, 3.8c,-195.8c ";
nslog (@ "% @", [celciusregex replaceallmatchesinstring: targetstring
delegate: self
replaceselector: @ selector (fahrenheitfromcelsius: contextinfo :)
contextinfo: Nil]);
// processing result: 97.7f, 38.8f, -320.4f
}< span>

// Convert degrees Celsius to Fahrenheit. 
-(Nsstring *) fahrenheitfromcelsius :( ogregularexpressionmatch *) amatch contextinfo :( ID) contextinfo 

Double Celcius = [[amatch substringatindex: 1] doublevalue]; 
Double Fahrenheit = Celcius * 9.0/5.0 + 32.0; 
Return [nsstring stringwithformat: @ "%. 1ff", Fahrenheit]; // return the replacement result. 

3. Example of using a name-based matching group. 

Ogregularexpression * RegEx = [ogregularexpression regularexpressionwithstring: @ ". +) \ ". +>" 
Options: ogrecapturegroupoption 
Syntax: ogrerubysyntax 
Escapecharacter: ogrebackslashcharacter]; 

Nsarray * matches = [RegEx allmatchesinstring: @ " 

If (matches! = Nil & ([matches count] = 1 ))
{< span>
ogregularexpressionmatch * match = [matches objectatindex: 0];
nsstring * result = [Match substringnamed: @ "imageurl"];
// result: ttp: // test.com/hello.jpg
}

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.