简体   繁体   English

python(re.sub())中的替换模式

[英]replacement pattern in python(re.sub())

The question 问题

在此处输入图片说明

Can someone please explain the process of the following re.sub() to me. 有人可以向我解释以下re.sub()的过程吗?

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 . 然后寻找一个数字,然后寻找另一个介于19之间的数字。 Now I am lost. 现在我迷路了。 What is the question mark for? 问号是什么? What does the \\d* do? \\d*什么作用? 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] \\d代表任何数字,其行为与[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. 当您使用[1-9] ,您将指定从1到9获得数字,这意味着排除了零。
  • 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. 因此,使用\\d*表示[0-9]任何连续数字或没有。
  • The ? ? is used to indicate using just one character or none. 用于表示仅使用一个字符或不使用任何字符。 So, using [1-9]? 那么,使用[1-9]? means try to find just one digit between 1 and 9 IF FOUND . 表示尝试在1到9的IFUND之间找到一个数字。
  • 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. 我认为应该如下所示(\\d*\\.\\d\\d[1-9]?)才能获得相同的结果。 I will try to explain this regular expression using this number 3.141500012 . 我将尝试使用此数字3.141500012解释此正则表达式。 \\d*\\. means find any number of digits that could be found before the dot which would match the 3. . 表示找到与点3.匹配的点之前可以找到的任意数量的数字。 then after that \\d\\d matches two digits after the dot which are 14 . 然后在\\d\\d匹配点后的两位数字14 Finally, the [1-9]? 最后, [1-9]? matches any digit between 1 and 9 if found which matches 1 in our example. 如果找到与我们示例中的1匹配的数字,则匹配1到9之间的任何数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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