iOS Regular expressions

Source: Internet
Author: User
Tags rounds

iOS can also use regular expressions in 4.0, and the functionality is quite powerful.

Once thought that he had mastered the regular expression, this 2 days to understand how complex the regular expression, there is a special thick book "Regular expression Primer classic."

The objective of the applet is to match the PGN in the game.

The rule is this: numbers indicate the first few rounds, followed by a small period, and then the red side moves, you can follow the commentary, then the Black law, you can follow the commentary. The commentary is placed in curly braces, and can be single-line or multiline.

1. Gun eight flat five Cannon 8 flat 5

{The red side of the first frame in the gun will go, Nie Qisheng also frame in the fight against the son force, tactical opponents. }

2. Cannon five into five like 7 into 5

3. Cannon two flat Five

{The cannon in the frame is also a positive, such as the change of the eight seven, then like 5 back 7, Red Square handsome house attacked,

Of course, if the red side is still in the fight against the gun, then lose the double cannon is difficult to have a role.

} Horse 8 in 7

... ...

It is easy to read the game manually, but it is not easy to describe it in a programming language, and the last regular expression is a heavenly book:

\d+\.\s+\w{4}\s* (\{(. | [\ r \ n]) *?\})? \s*\w{4}\s* (\{(. | [\ r \ n]) *?\})?

Put it in iOS and add the escape character, that's it:

\\d+\\.\\s+\\w{4}\\s* (\\{(. | [\\r\\n]) *?\\})? \\s*\\w{4}\\s* (\\{(. | [\\r\\n]) *?\\})?

Because the programmer also to extract the number of rounds, Red Square, red side commentary, Black square, black side commentary and other important information, so also in the above expression to add () This kind of grouping information, the final expression is more complex:

@ "(\\d+) \\.\\s+ (\\w{4}) \\s* (\\{(?:. | [\\r\\n]) *?\\})? \\s* (?:( \\W{4}) \\s* (\\{(?:. | [\\r\\n]) *?\\})?)?";

Before the program, with a small software on Windows Regexbuddy the regular expression test many times before passing, if directly on the machine debugging will be tired half dead.

Let's break the meaning of a simpler regular expression to understand:

\d+\. An integer followed by a small dot
\s+//1 + separators
\W{4}//4 character (s)
\s*//0 + separators
(\{(.| [\ r \ n])   *?\})? Comments on one or more lines of {} In curly braces, red side Gongfu commentary
\s*//0 + separators
(///In the last round, only the red side of the Gongfu can be
\W{4}//4 character (s)
\s*//0 + separators
(\{(.| [\ r \ n])    *?\})? Comment on one or more lines of {} In curly braces, gongfu commentary of Black side
)?

This involves the regular expression syntax:

\d matches any number, i.e. [0-9]

\d+ represents more than 1 digits

(\d+) forcibly add parentheses, grouping, quite put this value buffer, in the code with [myString Substringwithrange:[match Rangeatindex:1]] can be extracted out of the number of rounds

\. Indicates a small period

\s represents a delimiter, including spaces, tabs, and line breaks

\s* 0 or more separators

\w represents letters, numbers, and underscores, as well as Unicode characters, which are different in different languages.

\W{4} represents 4 non-whitespace characters

. Represents any one character, not including line breaks

. * denotes any number of characters and does not include line break

(.| [\ r \ n]) * denotes any number of characters, including line break, greedy scan

(.| [\ r \ n]) *? Represents any number of characters, including line breaks, lazy scan

(?:.|   [\\r\\n]) To (?: The beginning of the grouping information, indicating not to read into the buffer , to avoid the side effects of Rangeatindex calls, you will also encounter such a (?:

\{(.| [\ r \ n]) *?\} A comment statement placed in the middle of the curly braces, which supports multiple lines of comment because it contains a newline character

(\{(.| [\ r \ n])  *?\})? You can have no comments or 1 comments

Using the Nsregularexpression class to parse regular expressions in iOS, the main usage is:

NSString *regtags = @ "\\[(\\w*) \\s*\\\" (. *) \\\ "]\\s*\\n"; Well-designed regular expressions, it's best to test them in small tools first.

Nsregularexpression *regex = [nsregularexpressionregularexpressionwithpattern:regtags

Options:nsregularexpressioncaseinsensitive//You can add some options, for example: case insensitive

error:&error];

Performing a matching process

Nsarray *matches = [Regex matchesinstring:pgntext

options:0

Range:nsmakerange (0, [pgntext length])];

Use the following method to traverse each matching record

For (Nstextcheckingresult *match in matches) {

Nsrange Matchrange = [Match range];

NSString *tagstring = [Pgntext substringwithrange:matchrange]; The entire matching string

Nsrange r1 = [Match rangeatindex:1];

if (! Nsequalranges (R1, Nsmakerange (nsnotfound, 0))) {//by Time group 1 may not have found the corresponding match, use this method to judge

NSString *tagname = [Pgntext substringwithrange:r1]; The string corresponding to group 1

}

NSString *tagvalue = [Pgntext substringwithrange:[match rangeatindex:2]; The string corresponding to group 2

}

How to use in C #:

Regex reg = new Regex (@ "\d+\.\s+ (\w{4}) \s* (?: \ {(?:.| [\ r \ n]) *?\})? \s* (\w{4}) \s* (?: \ {(?:.| [\ r \ n]) *?\})?");
MatchCollection collection = Reg. Matches (Pgntext);
GroupCollection groupcollection;

foreach (Match match in collection)
{
Console.WriteLine (match. ToString ());
GroupCollection = match. Groups;
String redmove = Groupcollection[1]. Value;
if (!redmove.equals (""))
Chmoves.add (Redmove);
String blackmove = Groupcollection[2]. Value;
if (!blackmove.equals (""))
Chmoves.add (Blackmove);
}

Other posts to refer to:

The following page describes the greedy and lazy problems of regular expressions and some advanced usage

Http://blog.benhuoer.com/posts/crucial-concepts-behind-advanced-regular-expressions.html

How to use regular expressions to match/* in C language ... */such comments

Http://ostermiller.org/findcomment.html

There are some good regular expression books on the VERYCD, and it's not too late to grind and sharpen and write programs.

http://www.verycd.com/topics/2826047/

iOS 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.