Python Regular expression potential match
Question: I'm using the re module to validate IP address, this is my pattern:

`"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$"`

Is there a way to know if a string **_can become_** a potential match without
chaniging the pattern? for example: `"127.0.0."` is good or `"10.0"` however
`"10.."` is not good. I don't mean the `re.match` function, I want to know if
a string is not a match but it could be.

I need a function that will do something like this:

    
    
    import re
    p = re.potential("10.0","^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
    print p # True
    

**Edit:** the thing is I want to know if I can recognize a potential match and
use it to limit the wx.TextCtrl with the wx.EVT_CHAR Event, I didn't ask about
my pattern. right now I have implemented it like this:

    
    
    def OnChar(self,event):
        """event validation of ip"""
        key = event.GetKeyCode()
        text_value = event.GetEventObject().GetValue()
        length = len(text_value)
        numbers = True
        point = True
    
        if length:
            if length>2 and '.' not in text_value[-1:-4:-1]:
                numbers = False
            elif text_value[-1] =='.':
                point=False
    
        if Keys.is_numeric(key) and numbers:
            event.Skip()
    
        if Keys.equal(key,'.') and point:
            event.Skip()
    
        if Keys.is_moves(key):
            event.Skip()
    

This way the text the user enters can't be something not good, but is there a
way to do it with the re module?


Answer: If I understand you correctly, you want an expression that matches when
something can _become_ an IP address, in a way. Just make parts optional:

    
    
    ^(?:\d{1,3}(?:\.\d{1,3}){0,2}(?:\.\d{0,3})?)?$
    

[Demo](https://regex101.com/r/yM2kJ0/1): This matches `12`, `234.54`,
`23.53.12.5` and the empty string, but not `34.34..4` or `3546.34`.

**edit:** Less nested, thanks to Casimir et Hippolyte.

