简体   繁体   中英

replacement pattern in python(re.sub())

The question

在此处输入图片说明

Can someone please explain the process of the following re.sub() to me.

I am thinking the process is as following:

look for a "." then look for a digit then look for another digit that is between 1 and 9 . Now I am lost. What is the question mark for? What does the \\d* do? Why do we need to use raw string regex in this case?

If you want to understand the process, I can simply explain it to you. I don't know if this regular expression is doing what you want or not..

  • At first, the . is a special character in regex which means any character. But, we here want to use the dot character. In regex, this can be done by using escaping character \\ like so \\. . So, using . means any character and using \\. means a dot.
  • The \\d represents any digit and acts exactly like [0-9]
  • When you used [1-9] , by then you specified to get the numbers from 1 till 9 which means that zero is excluded.
  • We can use the asterisk * to choose zero or more characters. Unlike + which is used to choose one or more characters. So, using \\d* means any consecutive digits from [0-9] or none.
  • The ? is used to indicate using just one character or none. So, using [1-9]? means try to find just one digit between 1 and 9 IF FOUND .
  • The Parenthesis () is used for grouping the whole regular expression in one output.

If you want to know more about regular expression, here is an awesome cheat sheet. 在此处输入图片说明

NOTE:

I think the regex you have written in the question is not correct. I think it should be as follows (\\d*\\.\\d\\d[1-9]?) to obtain the same result. I will try to explain this regular expression using this number 3.141500012 . \\d*\\. means find any number of digits that could be found before the dot which would match the 3. . then after that \\d\\d matches two digits after the dot which are 14 . Finally, the [1-9]? matches any digit between 1 and 9 if found which matches 1 in our example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM