In general, we will use regular expressions for String Matching. When browsing the Internet today, we can see that someone uses regular expressions to check whether a number is a prime number (prime number ), this regular expression is very interesting to me, as shown in:
Regular expression for checking the prime number or not
To use this regular expression, you need to convert the natural number into multiple strings of 1. For example, 2 must be written as "11", and 3 must be written as "111 ", 17 "11111111111111111" should be written, which can be easily completed using some scripting languages.
At first, I was skeptical about this expression, but I carefully studied this expression and found it very reasonable. Below, let me show you how this expression works.
First, we can see that the expression contains "|", which means the expression can be divided into two parts:/^ 1? $/And/^ (11 + ?) 1 + $/
Part 1:/^ 1? $/. I don't need to talk about this part. It indicates that it matches the "empty string" and the string contains only one "1.
Part 2:/^ (11 + ?) 1 + $/, which is the key part of the entire expression. It can be divided into two parts: (11 + ?) And 1 + $, the first half is very simple, match the string starting with "11" and repeat 0 or n 1, the latter part means that the first half is used as a string to match the remaining strings once or multiple times (this sentence means that the number of the remaining strings 1 is returns an integer multiple of the number of strings ).
It can be seen that this regular expression is a non-prime number. To obtain a prime number, you must reverse the entire expression. Through the above analysis, we know that the second part is the most important. For the second part, let's give a few examples,
Example 1: Determine the natural number 8. We can know that 8 is converted into our format as "11111111". For (11 + ?), It matches "11", so there is still "111111", and 1 + $ matches the remaining "111111", because, the "11" mode appears three times in "111111". If the mode matches, true is returned. Therefore, the match is successful, so this number is not a prime number.
Example 2: determine the natural number 11. To convert to the format we need is "11111111111" (11: 1), for (11 + ?), It matches "11" (the first two 1), and there is still "111111111" (nine 1), while 1 + $ cannot match the "nine 1" for "11 ", because the "11" mode does not appear N times in the "nine 1" string. Therefore, our regular expression engine will try the following method, first matching "111" (first three 1 ), then we use "111" as the pattern to match the remaining "11111111" (eight 1). Obviously, "eight 1" does not match "three 1" multiple times. So the engine will continue to try again ...... All attempts may fail to match. Therefore, 11 is a prime number.
Through Example 2, we can obtain such an equivalent number calculation algorithm. The regular expression will match the number of integers that show "Two 1, an integer multiple of "three ones" and an integer multiple of "four ones ......, However, this is exactly the algorithm we need to calculate the prime number. Now everyone understands.
Next, we will use perl to use this regular expression to continuously output Prime Numbers: (I will not talk much about the perl syntax. Please pay attention to the anti-operator before the expression)
1 perl-e '$ | ++; (1 x $ _)!~ /^ 1? $ | ^ (11 + ?) 1 + $/& print "$ _" while + $ _'
In addition, let's make a difference. Based on the above method, we can even use a regular expression to verify whether a certain method has a solution, for example:
Binary equation: 17x + 12y = 51 the regular expression used to determine whether a solution exists is ^ (. *) 1 {16} (. *) 2 {11} $
Ternary equation: 11x + 2y + 5z = 115 the regular expression used to determine whether a solution exists is: ^ (. *) 1 {10 }(. *) 2 {1 }(. *) 3 {4} $