[英]Splitting up the digits in python using regex
I am trying to learn regex in python.我正在尝试在 python 中学习正则表达式。 I came across the "split" function where we have to give pattern and the string.我遇到了“split”函数,我们必须在其中给出模式和字符串。 I could understand the below output:我可以理解以下输出:
a = '123456789'
b = re.split("(4)", a)
print(b)
['123', '4', '56789']
But I could not understand this below output.但我无法理解下面的输出。 Why is "3" is not printing out in the output?为什么“3”没有在输出中打印出来? Can someone please explain?有人可以解释一下吗?
a = '123456789'
b = re.split("\d(4)", a)
print(b)
['12', '4', '56789']
You have told it to split at \\d(4)
, so it will find a part of your string that matches \\d(4)
.您已经告诉它在\\d(4)
处拆分,因此它会找到与\\d(4)
匹配的字符串的一部分。 That part is 34
, 3
for \\d
and 4
for (4)
.那部分是34
, 3
为\\d
和4
为(4)
If you split the string there, you get 12
in front of it and 56789
after it.如果在那里拆分字符串,则会在它前面得到12
在它后面得到56789
。
For the middle part, you have only put the 4
into a capturing group: (4)
, so only the 4
will be captured, not the 3
.对于中间部分,您只将4
放入捕获组: (4)
,因此只会捕获4
,而不是3
。
Things to try next for a better understanding:为了更好地理解,接下来要尝试的事情:
\\d4
as the delimiter, without any capturing group使用\\d4
作为分隔符,没有任何捕获组(\\d4)
as the delimiter, including the 3 in the capturing group使用(\\d4)
作为分隔符,包括捕获组中的 3Solutions available at https://ideone.com/3yCKGF https://ideone.com/3yCKGF提供的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.