[英]replacement pattern in python(re.sub())
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
. 然后寻找一个数字,然后寻找另一个介于
1
和9
之间的数字。 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..
我不知道此正则表达式是否正在执行您想要的操作。
.
.
is a special character in regex which means any character. \\
like so \\.
\\
这样\\.
来完成\\.
. .
.
means any character and using \\.
\\.
means a dot. \\d
represents any digit and acts exactly like [0-9] \\d
代表任何数字,其行为与[0-9]完全相同 [1-9]
, by then you specified to get the numbers from 1 till 9 which means that zero is excluded. [1-9]
,您将指定从1到9获得数字,这意味着排除了零。 *
to choose zero or more characters. *
选择零个或多个字符。 Unlike + which is used to choose one or more characters. \\d*
means any consecutive digits from [0-9]
or none. \\d*
表示[0-9]
任何连续数字或没有。 ?
?
is used to indicate using just one character or none. [1-9]?
[1-9]?
means try to find just one digit between 1 and 9 IF FOUND . ()
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.