Search code examples
c++regexwxwidgets

Regex, capture 4 numbers surrounded by . or line start/end


I'm trying to capture any 1-2 digit numbers surrounded by '.' or the beginning/end of a line.

E.G

  • 1.0.4.11

  • 71.11.11.11

  • 0.11.0.0

Are valid and:

  • 1.
  • 1111
  • 11.11.11.
  • 01.10

are not valid

Right now I've got (?<=\.|^)\d{1,2}]?(?=\.|$) which will capture the numbers correctly but will also capture groups such as 11.. or 1.11.

I need to extend this regex to basically verify that it is always in the format x.x.x.x where x is 1-2 digits.

For additional information, this regex will run using the wxWidgets regex class but I believe that's the standard regex parser.

NOTE

For anyone using this as reference... Using wxWidgets, the wxRegex class must be constructed with the wxRE_ADVANCEDflag as by default it uses a basic/fast implementation that does not include quantifiers(?*) which are used in this expression.


Solution

  • You can make it less generic by specifically look for your 4 groups between start and end of string (you can remove the \.? if you never have . at the start or end):

    ^\.?\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1,2}\.?$

    See in Regex101