Some questions about the expressions in the Python

Source: Internet
Author: User

Hear a word today, feel very sensible--"When you encounter a problem, think of using regular expression to solve, it becomes two questions"

This also explains from the side that the regular expression is more difficult to understand. Below I will summarize in an easy-to-understand way some of the recent regular expression problems

I. The "problem" of greedy matching1. Too greedy to "break the vegetables"

The matching mode is R ". * (\d*). *", in this mode, (\d*) represents a greedy match, matching the preceding character \d, that is, the number 0 or countless times, but in our string, \d matches countless times the case is nonexistent (actually for the search method, it will find "1" if " 11 "Does not meet the" 1 "match countless times, then it will choose to match" 1 "0 times, match 0 times directly returned), therefore, (\d*) is to match the preceding number 0 times, since it is 0 times, then the contents of the returned packet should be empty string" ', Group (1) and groups () Can verify this, such as group (1) representing a string that returns a 1th grouping, groups () returns a tuple containing all the group strings, since there is only one grouping in this example, and the group number is 1, so we can see that the empty string '

But, someone asked, why does the. Group () see that it matches the entire string? In fact, our grouping--matching the previous number 0 times--is not going to do anything, it's equivalent to a matching pattern of r ". * *", do not believe please see, the result is not exactly the same?

For example, the matching model is very love of wealth of friends, my bank balance is a string, one day he suddenly borrowed money from me, man, can I borrow a lot of money?

I asked, how much? He said, of course, the more the better Ah, hehe, this greedy person. I check the balance of the account, a look, only 100,000 left, of course, choose not to lend him! So I knew he was insatiable, and if I lent him 100,000, he would ask me 200,000, 300,000 ... Even more, so, for greed's friend, I refused--at last he did not get a penny, got empty (empty string "). So the friend took a look at my account balance (search) and went off in the dingy

2. "The suspicion of heavy" greed

One day, this friend came again, this time his suspicion has increased some, he borrowed money from me, man, can I borrow a lot of money?

I took out my cell phone to check the balance of the account, which said about the "100,000", the friend is not to give up, obviously is not satisfied with his greed, he is also the stalker I do not put, I have to check the specific details to him to see, and to be accurate to the point after a few points, for the first time, I told him He got the empty (empty string "), the second time, when I query the specific details, the above shows the" 101283.88 "Yuan, I again rejected him, he took another empty, this is the role of FindAll (), return all can match to the substring

two. Greedy "Epiphany"1. "Please give the greed to others"

My friend in the case of many times to borrow money without fruit, after my earnest persuasion, finally a little wake up, he intends to make a clean, and do a ascetic person

One day, he came to me suddenly, man, can you lend me some money, two-digit

I listen, MA, this change quite big, got, borrow him 99 dollars or no problem, is about to take out 99 yuan, he suddenly stopped me, said to see my account balance

Startled out in a cold sweat, but still embrace the attitude of trust him, the phone to him, he looked at the account balance, "101283.88" yuan, slowly said, you can lend me 8 cents 8?

Of course--I was relieved, and looked at him with suspicion, by the way, where is your greed?

He said without thinking, "I gave the greed to others-our greedy family, the generational in front of me". * "with him, I can not bear the notoriety of greed, I will only find the most back two digits, for your balance, should be the least

2. Non-greed-"Everyone is not greedy, to achieve reciprocity"

One day, my friend came to see me happily, said that his predecessors also reformed, began slowly accustomed to the poor, accustomed to meet

I know, it's my friend's credit. When he was harmed by his desires, he conveyed this thought to his predecessors like a preacher.

His predecessors took his baptism and deliberately drew a "?" on his hat. Mark, which is his unique symbol of self-restraint.

Of course, this time my friend also came to borrow money from me, man, can you lend me some cash, two-digit, just like the tone of the last time?

I still have "199.88" yuan at this time, why is there only so little left? It's because I recently went to the stock

So, I took out 99 yuan, the biggest two-digit number to him, told him--Quick accept, this is you deserve

Now he's not looking at my balance details, so I took a glance at the balance. Indeed, he is improving day by day ...

Some questions about the expressions in the Python

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.