-2

I have this regex (I'm very, very poor on regex, sorry!):

.*

It catches every tag I put on git, ex. 3.18.0.1-COLL or 4.50.1-TEST

But I need catch only this format:

n.n.n

So, for example 3.18.0 or 1.2.3, 3 digits with 2 points inside. Digits from 0 to 99999.

How can I write that regex?

sineverba
  • 5,059
  • 7
  • 39
  • 84

3 Answers3

1

^\d{1,5}\.\d{1,5}\.\d{1,5}$

\d{1,5} Will match 1 to 5 digits next to each other, \. will match a dot literally. Wraping a regex pattern with ^ and $ will block and preceding and following characters from matching the pattern.

Also regex101 is a great tool.

Alp Bilgin
  • 56
  • 6
0

This should do it...

[0-9]{1,5}\.[0-9]{1,5}\.[0-9]{1,5}

[0-9] is basically match an number between 0 and 9.

{1,5} means to repeat the number matching 1 to 5 times (9, 99, 999, 9999, 99999).

\. just checks for the full stop/dot/decimal/point whatever you want to call it.

And here is a visual representation...

enter image description here

The above image is take from https://www.debuggex.com/ which I found to be a super useful tool when learning regex.


You mentioned "Digits from 0 to 99999" however, if you want to match numbers greater than 999999 such as 999999123 you can modify the regex above to...

[0-9]+\.[0-9]+\.[0-9]+

Which translates to...

[0-9] match an number between 0 and 9.

+ means repeat the number matching infinite times.

\. just checks for the full stop.

Levi Cole
  • 3,561
  • 1
  • 21
  • 36
0

Pattern: ^(?:\d{1,5}\.){2}\d{1,5}$

Explanation

  1. ^ - Beginning of the string

  2. (?:\d{1,5}\.) - Captures upto 5 digits (\d{1,5}) followed by a period (\.)

  3. {2} - Capture exactly 2 occurrences of the previous group

  4. \d{1,5} - Captures upto 5 digits

  5. $ - End of the string

Example

Text        -> 3.18.12345
Regex Match -> 3.18.12345

Text        -> 3.18.12345.1.2-WHATEVER
Regex Match -> no match

Text        -> 3.18
Regex Match -> no match
Ashyam
  • 666
  • 6
  • 13
  • No, this matches also "1.2.3-COLL" that I don't want – sineverba Sep 06 '21 at 06:58
  • Correct me if I'm wrong, you want to only match the string if the string is EXACTLY in `n.n.n` format and it shouldn't match string which contains `n.n.n` in it? If thats the case I think the following should work: `^(?:\d{1,5}\.){2}\d{1,5}$` – Ashyam Sep 06 '21 at 09:36
  • Need to match only 1.2.3 and not 1.2.3-COLL or 1.2.3-TEST or 1.2.3-WHATEVER – sineverba Sep 06 '21 at 13:24
  • The pattern I mentioned in my last comment does just that. Did you try it? The `^` and `$` marks the start and end of the string. So it will ONLY match 1.2.3 and not 1.2.3-COLL or 1.2.3-TEST or 1.2.3-WHATEVER. I'll update the answer if it works as expected. – Ashyam Sep 06 '21 at 17:26