Splitting a string similar to ip addresses using regex in Python -


i want have regular expression split on seeing '.'(dot)

for example:

input: '1.2.3.4.5.6' output : ['1', '2', '3', '4', '5', '6'] 

what have tried:-

>>> pattern = '(\d+)(\.(\d+))+' >>> test = '192.168.7.6' >>> re.findall(pat, test) 

what get:-

[('192', '.6', '6')] 

what expect re.findall():-

[('192', '168', '7', '6')] 

could please in pointing wrong?

my thinking - in pattern = '(\d+)(\.(\d+))+', initial (\d+) find first number i.e. 192 (\.(\d+))+ find 1 or more occurences of form '.<number>' i.e. .168 , .7 , .6

[edit:] simplified version of problem solving. in reality, input can be-

192.168 dot 7 {dot} 6 

and expected output still [('192', '168', '7', '6')].

once figure out solution extract .168, .7, .6 patterns, can extend dot 168, {dot} 7 patterns.

since need find numbers, regex \d+ should enough find numbers separated other token/separator:

re.findall("\d+", test) 

this should work on of cases:

>>> re.findall("\d+", "192.168.7.6") ['192', '168', '7', '6'] >>> re.findall("\d+", "192.168 dot 7 {dot} 6 | 125 ; 1") ['192', '168', '7', '6', '125', '1'] 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -