While splitting String
I found different result using regex
. Consider the following examples -
- using
string.split()
gives me alist
excluding the delimiter-.
var3 = "black.white"
print(var3.split('.'))
# result
# ['black', 'white']
- using
re.split()
using this regex-([.] *)
gives me alist
including the delimiter -.
var3 = "black.white"
print(re.split('([.] *)', var3))
# result
# ['black', '.', 'white']
- using
re.split()
with this regex-[.] *
without the grouping parenthesis()
gives me alist
excluding the delimiter -.
var3 = "black.white"
print(re.split('[.] *', var3))
# result
# ['black', 'white']
I know there is something to do with the grouping parenthesis ()
but couldn't understand why. Therefore I have these three question in mind -
- Why
string.split()
doesn't keep the delimiter - Why
re.split()
keeps the delimiter - Why grouping parenthesis
()
inregex
makes the difference
note: I am new to python
and regex