You need to make a regular expression to check the string denoting the version of the application. The string has the form XYZ, where X, Y, Z can be any integers, as well as * (except X). After the * character, there should be no further line. Numbers should not be 0. Examples of valid strings:

  • 12.2323.2
  • 0.0.3
  • 0.0.0
  • 34.0.3
  • 23.2. *
  • four.*

Examples of invalid lines:

  • 34a.34.1
  • *
  • 34. *. 3
  • 57. *.
  • d3.43.3
  • 0004. *
  • 1.02. *

I did not do such a regular season.

This is not a learning task.

  • four
    "I did not work out" - just words. Therefore, close as a training task. Try it yourself and post the result . We will see if this is just a piece from the Internet, and not a real attempt. - user207618
  • @Other, if I could write such a regular season myself, this question would not be here - mirypoko
  • He could and tried - different things. You have not even tried. - user207618
  • @Other what do you want from me? So I laid out a set of non-working regulars? - mirypoko
  • 2

2 answers 2

Naive implementation of what you want:

^(0|[1-9]\d*)\.(\*|(0|[1-9]\d*)\.(\*|(0|[1-9]\d*)))(\r)?$ 

I got it by following such conclusions. First we create a regular checker that checks the string against the ABC pattern, it is simple:

 ^\d+\.\d+\.\d+(\r)?$ 

here \d+ is any number sequence, \. - point (must be escaped, yes), ^ - the beginning of the line, (\r)?$ - the end of the line, taking into account both \r\n , and just \n

Further, instead of the last block of digits, there can be a single asterisk, replacing \d+ with (\*|\d+) (the asterisk must also be escaped):

 ^\d+\.\d+\.(\*|\d+)(\r)?$ 

Also, the asterisk can be instead of the last two blocks, similarly to the previous one, change \d+\.(\*|\d+) to (\*|\d+\.(\*|\d+)) :

 ^\d+\.(\*|\d+\.(\*|\d+))(\r)?$ 

Well, it remains to exclude numbers with leading zeros, i.e. This is either a separate zero 0 , or non zero + any number of digits [1-9]\d* . Replace all three blocks \d+ with a construction (0|[1-9]\d*) , we get the final version.

     ^(0|[1-9]\d*)\.(\*|((0|[1-9]\d*)\.(\*|(0|[1-9]\d*))))$ 

    A little bit of explanation:

    the leading zero is cut off with the following expression 0|[1-9]\d* . Those. We allow either one zero or 1-9, and any number of any digits behind it.

    * so controlled \*|((0|[1-9]\d*)\.(\*|(0|[1-9]\d*))) or asterisk, or everything else.

    Test for regex101 https://regex101.com/r/41aBtQ/1