-2

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.

4
  • 4
    Wait, what's wrong with test.split('.') if all you want to do is split on the dots? Don't swat flies with a sledgehammer.
    – Chris
    Commented Sep 10, 2017 at 23:58
  • I had guessed this would come, but my problem is different from what I am asking. basically, instead of numbers, there is complex strings. Instead of '.' there can actually be "dot" i.e. 192 dot 168 dot 7 dot 6, like this and their combinations etc. Commented Sep 11, 2017 at 0:01
  • 3
    In that case, can you update your question and post some of the actual data you want help with? It's not much use for us to give you solutions to a different problem from what you're trying to solve.
    – wkl
    Commented Sep 11, 2017 at 0:02
  • I tried this regex and it gave me the result you wants, (\d+)[\.]*.
    – Fady Saad
    Commented Sep 11, 2017 at 0:04

1 Answer 1

0

Since you only need to find the numbers, the regex \d+ should be enough to find numbers separated by any other token/separator:

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

This should work on any of those 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']
1
  • 1
    Code-only answers are discouraged because they do not explain how they resolve the issue in the question. Consider updating your answer to explain what this does and how it addresses the problem. Please review How do I write a good answer Commented Sep 11, 2017 at 0:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.