iOS can also be used in regular expressions in 4.0.

Source: Internet
Author: User
Tags rounds

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

Once thought oneself already mastered regular expression, these 2 days only then understood regular expression has how complex, originally also has specially thick a book "The Regular Expression Entry Classics".

The goal of the applet is to match the PGN games in the same section.

The rule is: the number represents the first round, there is a small period, and then Red Square law, can be with the commentary, and then the black side of the law, can follow the commentary. The commentary is placed in curly braces, can be single, or multiple lines.

1. Cannon eight level five guns 8 flat 5

{Red side of the first rack in the cannon will go, Nie Qisheng also in the gun to fight the son force, tactical opponents. }

2. Cannon five into five like 7 into 5

3. Cannon two flat Five

{The cannon is also in the same frame, such as the change of eight into seven, then like 5 retreat 7, Red Square handsome mansion was attacked,

Of course, if the red party is still in the frame of the gun, then the loss of two guns will be difficult to work.

Horse 8 into 7

... ...

Manual is easy to read games, but it is not easy to describe in the programming language, the last written regular expression is a piece of heavenly book:

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

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

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

Because the programmer also extracts the 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* (The \\{(?:. | [\\r\\n]) *?\\})?)?";

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

First, break the meaning of the simpler regular expression and understand:

\d+\. An integer followed by a small dot
\s+//More than 1 separator
\W{4}//4 characters
\s*//More than 0 separator
(\{(.| [\ r \ n])   *?\})? One or more lines of notes placed in curly braces {}, Shifa commentary of Red Party
\s*//More than 0 separator
(////In the last round, you can only have red side Shifa
\W{4}//4 characters
\s*//More than 0 separator
(\{(.| [\ r \ n])    *?\})? One or more lines of comments placed in the curly braces {}, Shifa commentary of the Black side
)?

This involves the regular expression syntax:

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

\d+ says more than 1 digits.

(\d+) forcibly with parentheses, grouped, quite to buffer this value, in the code with [myString Substringwithrange:[match Rangeatindex:1]] can extract the number of rounds

\. Indicates a small period

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

\s* 0 or more delimiters

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

\w{4} represents 4 Non-white-space characters

. Represents any one character, excluding line breaks

. * represents any number of characters and, of course, does not include line breaks.

(.| [\ r \ n]) * represents any number of characters, including line breaks, greedy scans

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

(?:.|   [\\r\\n]) To (?: The beginning of the packet information, said not read into the buffer, to avoid the rangeatindex call when the side effects, and then encountered this (?: Writing

\{(.| [\ r \ n]) *?\} A comment statement placed in the middle of curly braces that supports multiline comments because it contains line breaks

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

Using the Nsregularexpression class in iOS to parse regular expressions, the main uses are:

NSString *regtags = @ "\\[(\\w*) \\s*\\\" (. *) \\\ "]\\s*\\n"; Well-designed regular expressions, preferably in small tools.

Nsregularexpression *regex = [nsregularexpressionregularexpressionwithpattern:regtags

Options:nsregularexpressioncaseinsensitive//can also add some options, such as: case-insensitive

error:&error];

The process of performing a match

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]; Entire matching string

Nsrange r1 = [Match rangeatindex:1];

if (!  Nsequalranges (R1, Nsmakerange (nsnotfound, 0)) {//From time Group 1 may not find the corresponding match, in this way to determine nsstring *tagname = [Pgntext  SUBSTRINGWITHRANGE:R1]; The string corresponding to group 1

}

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

}

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

Also can refer to the post:

The following page describes the question of greed and laziness for 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 the/* in C language ...

Http://ostermiller.org/findcomment.html

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

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

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.