I am trying to learn regex in python. I came across the "split" function where we have to give pattern and the string. 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? 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)
. That part is 34
, 3
for \\d
and 4
for (4)
.
If you split the string there, you get 12
in front of it and 56789
after it.
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
.
Things to try next for a better understanding:
\\d4
as the delimiter, without any capturing group(\\d4)
as the delimiter, including the 3 in the capturing groupSolutions available at https://ideone.com/3yCKGF
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.