# RegEx example ZZ

Source: Internet
Author: User

As shown in the Reddit recommendation article. The followingCodeRun in the CMD window. If running in bash, single quotes and double quotation marks must be reversed.

Perl - Wle " Print 'prime 'if (1 X shift )!~ /^ 1? \$ | ^ (11 + ?) \ 1 + \$/ " < Any number >

The Python code is:

Import Re
Def Is_prime (Num ):
Return Not Re. Match (R " ^ 1? \$ | ^ (11 + ?) \ 1 + \$ " , " 1 " * Num)

Ruby has the blood of Perl, so the code is almost the same as the Perl version (in fact, it doesn't matter, the key is the same RegEx ):

Ruby - E " Puts 'prime 'If (' 1' * Argv. Shift. to_ I )!~ /^ 1? \$ | ^ (11 + ?) \ 1 + \$/ " <Any number>

The judgment method is very interesting. Represent the given number in a single input format. For example, 5 is 11111. Then, starting from 2, we can calculate a number to check whether the number of 1 represents a multiple of any of them. For example, if the given number is 4, the corresponding value is 1111. Then 1111 matches ^ 11 \ 1, that is, it matches ^ (11 + ?) \ 1 + \$. It is a multiple of 2. Therefore, 4 is not a prime number. When the given number is 7, the corresponding value is 1111111. The GE string cannot match 11 \ 1 +, cannot match 111 \ 1 +,..., and cannot match 1111111 \ 1 +. Therefore, 7 is the prime number.

Let's explain the structure of RegEx:
^ 1? \$, Matches an empty string or "1 ". In other words, 0 and 1 are not prime numbers.

(11 + ?) \ 1 +, (11 + ?) Match a string of 2 or more characters, such as "11... 111. Note the question mark, indicating that the match is a point-to-point match, not a greedy match. This is important, otherwise (11 +) can match all strings with a length greater than 2 and in the shape of "11... 1. The backtracing reference \ 1 indicates matching the previously matched string, that is, (11 + ?). The eye-catching pen comes: \ 1 + indicates matching one or more previously matched (11 + ?). In other words, the matching string with a length of N * m, where n is greater than 0, and m is (11 + ?) The matching length. Because Perl's RegEx is automatically traced back, (11), (111), (1111)... will be automatically attempted.

In addition to being beautiful and exquisite, this code can be used to help you understand.RegExWhat is the value of yiwai? Efficiency? Obviously not high enough. Practical? I'm afraid there are not many. However, this code implies an important programming idea: coding. We can express a problem in some way, and then seek a solution based on this expression. This method seems simple but widely used. We can express all types of automatic machines with strings, so we can easily find many profound properties. We use the state machine to describe the system specifications, and then express the state machine logic formula. Therefore, we can automatically verify whether the system specifications have logical defects and whether there are security problems. Furthermore, the entire computing system on which we rely for a living is also based on coding: all the data commands are eventually converted0And1. Coding is essential to the computing theory. From the geder theorem to the Turing Machine Theory to the theory of automation to the complexity of computing, I don't know how many great proofs rely on coding.

A little more. Perl Of RegEx Matching is NP-complete . Here we have proof . Sudoku ( Sudoku ) NP-complete Problem. So we can write RegEx Expression. The key is to convert the problem surface of a Sudoku into matching strings. Niu Ren Abigail Given One solution . Abigail It is also the author of the above Code for determining the prime number. In fact, we can also convert the RegEx allocation problem into a data independence problem, and then use data independence.ProgramTo implement matching. Which of the following busy bosses can try it.

Http://blog.csdn.net/g9yuayon/archive/2007/03/19/1533798.aspx

Related Keywords:

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.