I want to have a regular expression which will split on seeing a '.'(dot)
For example:
Input: '1.2.3.4.5.6'
Output : ['1', '2', '3', '4', '5', '6']
What I have tried:-
>>> pattern = '(\d+)(\.(\d+))+'
>>> test = '192.168.7.6'
>>> re.findall(pat, test)
What I get:-
[('192', '.6', '6')]
What I expect from re.findall()
:-
[('192', '168', '7', '6')]
Could you please help in pointing what is wrong?
My thinking -
In pattern = '(\d+)(\.(\d+))+'
, initial (\d+)
will find first number i.e. 192
Then (\.(\d+))+
will find one or more occurences of the form '.<number>'
i.e. .168
and .7
and .6
[EDIT:] This is a simplified version of the problem I am solving. In reality, the input can be-
192.168 dot 7 {dot} 6
and expected output is still [('192', '168', '7', '6')]
.
Once I figure out the solution to extract .168
, .7
, .6
like patterns, I can then extend it to dot 168
, {dot} 7
like patterns.
test.split('.')
if all you want to do is split on the dots? Don't swat flies with a sledgehammer.