Regular Expressions and mathematics (equations, linear equations) _ Regular expressions

Source: Internet
Author: User
The regular expression is as follows:
Copy Code code as follows:

^1?$|^ (11+?) \1+$ can determine prime numbers (in the form of n 1, n is the size of a number.) For example, 5 converts to a 11111;3 conversion to a 111;2 conversion to 11. )

What is prime?
Early Middle School of the bar. What our teacher taught us was "prime". Look at the concept:
Prime numbers are also called primes. In a natural number greater than 1, the number that cannot be divisible by other natural numbers except for 1 and the integer itself.
In other words, only the natural number of two positive factors (1 and oneself) is prime. A number larger than 1 but not prime is called a composite. 1 and 0 are neither prime nor composite.
What is the meaning of this regular expression?
"^1?$|^ (11+?) \1+$ "The Middle with" | "separate. "|" In the regular grammar, the expression "or", acting on its two cells. (still not clear to see below, understand to skip this paragraph below)
Copy Code code as follows:

For example, "AB|CD" can match "AB", can also match the "CD", meaning that except "AB" is "BC", if you want to match the "Abd", "ACD" that "|" Scope to change, add a range
Change to "A (b|c)" (Match result allocation group) or "A (?: b|c) d" (matching results are not assigned to the group, more efficient).

Continue just the regular, divided into two branches, one is "^1?$" and "^ (11+?) \1+$ ". The regular of "^" in the grammar, except in the brackets "[]" is the meaning of the beginning, in the brackets in the expression is not.
The first branch "^1?$" matches "1" or "" (empty string).
Second Branch "^ (11+?) \1+$ ", first look in the brackets" (11+?) "The match is the character" 1 "followed by" 1+ "is 1 to countless 1. The "?" question mark in the back indicates that it is not greedy, that is, to match as little as possible.
Then, looking back in "\1+", "\1" represents the result of referencing the first group that has been matched. Which is the result of the first "()" bracket match. The same "\2" is the result of the second bracket capture. (Hint: The above mentioned "(?:) The writing is not to assign the group, so that the reference, you can not refer to the ")
"+" is 1 to countless. We can see this expression in this way. "(11+?) "As 1+n in mathematics, where n is a positive integer greater than 0." The "\1+" outside is the number of references to the previous group. Understood as M times, where M is a positive integer greater than 0.
The whole expression is (1+n) *m. Because N, M is greater than 0, then the 1+n must be greater than 1, the minimum is 2, the maximum is infinite, M is the smallest 1, the maximum is infinity.
Then, any multiple greater than 0 of a positive integer greater than 2 is always a composite number, or not a prime number.
Go back and look at the expression. The matches are 0 or 1 string "1", which is the number 0, the number 1. And all the other composite numbers. The entire expression, if the successful match is a non prime, if mismatch is prime. That's the right thing to do.
Copy Code code as follows:

if (Preg_match/^1?$|^ (11+?) \1+$/i ', $subject)) {
#不是素数
} else {
# is prime
}

Tip: This identification is a prime method only study learning, can not be used in the formal program, the string is too long, will cause very much fear of backtracking.
English Blog Address: http://blog.stevenlevithan.com/archives/algebra-with-regexes
In the above blog, there are two equations and regular expressions, and we'll look at them together.
• Two-Yuan equation 17x + 12y = 51, its expression "^ (. *) \1{16} (. *) \2{11}$". Very well understood. "(. *)" is 0 to countless "." Point number. (Here is the following, in fact, "." The point is to indicate the character "1")
That is, 0 to countless 1, followed by "\1" reference once. The back "{16}" is 16 times. Action on the front of the "\1", that is, 16 references. Plus the beginning of the "(. *)" altogether exactly 17 times. The latter one will not say, like this.
The regular engine then attempts to "(. *)" 0 to countless characters "1", 0 Characters "1", 1 Characters "1", 2 Characters "1" have been added. Until you succeed, try to finish the maximum number of all characters "1" (51 characters "1").
• Two or three-yuan formula 11x + 2y + 5z = 115, its expression is "^ (. *) \1{10} (. *) \2{1} (. *) \3{4}$", understood just like the one above. Note that "\2", "\3" is worth the 2nd, the 3rd bracket captures the content, don't look at the eye.
——————-Split Line ——————
Some of the interesting math problems above are to convert integers to the corresponding number of characters "1". The following is the conversion to binary numbers.
Eat first, then write later.

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.