Python was the first programming language I had access to, and though it was simple enough, it was difficult for me to get into the programming language at the time, so I just learned some basic Python syntax and didn't know much about it in a little bit. However, so far, I have been in touch with the programming language for a year, during the Python various features, all kinds of packages have seen, learned. However, the regular expression of this piece is still smattering, plus a period of time to help Congo finishing < in-depth python3> of the Chinese maintenance version, gather on the national Day holiday, so to simply talk about this regular expression module--RE.
The first is the substitution of characters in the string, if the use of native Python method, generally is the Replace method, the following we use the Re.sub method to compare with it.
First case: Our goal is to replace Road with Rd.
It seems that the Replace method works well and completes the task I have given. What if it is the following example?
There is obviously a problem here because the string contains broad, which also has a road four characters, but our goal does not require it to be replaced. Here it was also replaced by the BRD. , which is sufficient to illustrate the limitations of the Replace method. Of course, if we design properly, this method can also be used.
With slices, we can do this by simply replacing the four characters in the last face. However, this algorithm also has limitations if we want to replace Street with St. , should we keep the last six characters? This way, each substitution modifies the code and is prone to errors, which can be cumbersome for debugging.
So let's try the Re.sub method.
Note the first argument, ' road$ ', where the dollar symbol represents the end of the string, that is, match the end of the ROAD character. Similarly, ^ represents the beginning of a string. I quickly found that sometimes the ROAD is not necessarily at the end of the string, such as: s = ' BROAD ROAD apt.3 ', at this time, the above method does not apply, it doesn't matter, we also have \b.
Isn't it amazing? \b On the left means that the left side is a space, and on the right side is a space on the right side, so \b on both sides means that road is a separate word. That is to replace the Independent Road with Rd. This is the same as our goal.
Second case: matching of Roman numerals
In Roman numerals, the numbers are represented by a combination of seven letters.
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
Here are a few general rules that make up Roman numerals:
Most of the time, use the word typeface overlay to represent the numbers. I is 1, II is 2, and III is 3. VI is 6 (in one view, is "5 and 1" of the combination), VII is 7,VIII is 8.
A maximum of 10 characters (I,x,c and M) can repeat up to three. In order to represent 4, you must subtract one with the next larger number of the same digit 5来. It is not possible to use IIII to represent 4, but should be IV (meaning 5 smaller than 1). 40 do XL (50 smaller than 10), 41 write do xli,42 write do xlii,43 write xliii,44 write do XLIV (than 50 small 10 and less than 5 small 1).
Sometimes that means the opposite. In order to represent an intermediate number, you need to subtract from a final value. For example: 9 need to subtract from 10来: 8 is VIII, but 9 is IX (less than 10 small 1), not VIII (I character cannot repeat 4 times). 90 is xc,900 is cm.
A character that represents 5 cannot recur in a number. 10 can only be represented by X, not by VV. 100 can only be expressed in C, not LL.
Roman numerals are calculated from left to right, so the order of the characters is very important. The DC represents 600, and the CD is completely another number 400 (500 smaller than 100). CI is 101,ic not a Roman number (because you can't subtract 1 from 100, you can only write xcix, which means less than 100 small 10, and 10 smaller 1).
For the number of thousand matches:
We set the match pattern to be three m, where '? ' This parameter is optional, which means three optional m parameters.
When the first match is M, the match begins with the ^ (beginning of the string), matches to one m, and the other two because it is optional, so skips and then matches to $ (end of string), matching completes and returns a matching object. Second, the third match of the Army succeeded. On the fourth match, because it can only match up to three M, and give four m, so when the match three M is found, the search is not found, so the match fails, return none.
It is worth noting that because three parameters are optional, an empty string can also be matched.
For the number of hundred matches:
100 = C
200 = CC
300 = CCC
400 = CD
500 = D
600 = DC
700 = DCC
800 = DCCC
900 = CM
The following four matching modes are therefore available:
CM
CD
There can be between 0 and 3 characters C (0 representing thousands for 0).
D is immediately following the 0 to 3 characters c.
The latter two can be combined into one of the following:
An optional D, followed by a 0 to 3 character C.
In this case, the matching pattern contains thousands and hundreds. | Is the meaning of a logical operation, which is used when there are several cases in parallel, and when one of the conditions is met, the subsequent condition is stopped.
It can also be found that null characters can also be matched.
In the same vein, we analyzed the 10-bit and single-digit laws that could be matched.
The regular expression is very powerful, but it is not the correct answer to every problem. You need to know more about which situations are appropriate for using regular expressions. At some point it can solve your problem and sometimes it may bring more problems.
Reservation question:
1. "115.28.66.99[port=8080]", this string indicates that 8080 port of the server with IP address 115.28.66.99 is open, use the program to parse this string, and then print out "The port of the server with IP address = * * * is open".
2. "115.28.66.99[port=21,type=ftp]", this string indicates that the 21 port of the server with IP address 115.28.66.99 is provided with an FTP service, where if the ", Type=ftp" section is omitted, the HTTP service is the default. Please use the program to parse this string, and then print out the "Port of the server with IP address = * * * * * * * * * * * * *"
Python Regular expression module (re-module)