Is there a generic way to wrap an existing regex to also accept partial matches?
For example verifying an IPV4 address one can use ((?:(?:25[0-5]|2[0-4]\d|1?\d{1,2})(?:\.(?!$)|$)){4})
Now this works for
But not for
I want to provide a generic way to wrap the existing regex (or any else) without modifying it into a new regex that accepts any amount of partial ip addresses.
UPDATE: The intention of this is to use it as a mask for a text field thus I need to allow partial strings otherwise it would only work by inserting the complete IP at once e.g. via pasting from clipboard.
I suggest:
^(?:\b\.?(?:1\d{0,2}|[3-9]\d?|2(?:[0-4]\d?|5[0-5]?|[6-9])?|0|$)){0,4}$
demo
Feel free to change the last quantifier to {1,4}
if you do not want the empty string to be valid.
Notices:
$
to allow a trailing dot. It is more efficient to do that and less problematic than making the whole group optionalIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With