Regular expression: 2 digits followed by .. or - or __ followed by 2 digits

I have a regex that I need to implement. rules

2 digits followed by 2 .. or 2 -- or 2 __ followed by 2 digits it cannot be empty it cannot have only one pair (ie 01) The string can be up to 1000 characters in length. ie, 01..02--03 or 01..01 or 01--02--03--04--05..06 and so on 
+4
source share
4 answers

Here's how I do it:

 \d{2}(?:(?:[.]{2}|-{2}|_{2})\d{2})+ 

Explanation: Two digits, followed by one or more occurrences of two identical characters, consisting of a period, hyphen or underscore, and then two more digits.

If you need to fix this, you can add ^ in front and $ at the end.

The reason I prefer to use {2} instead of writing about myself (that is, repeating the same character) is because it allows you to increase the number. As the number gets larger, counting the number of repeated characters will become more and more difficult.

In addition, depending on your font and screen size, some characters may be visually combined into one long character, making it difficult to figure out how many of them are in sequence. The undescore symbol is a prime example of this, consider: _____ How many underscores are this? Compare and contrast this with this expression: _{5}

+6
source

This should do the trick:

 ^(?:\d{2}(?:--|\.\.|__))+\d{2}$ 
  • At the beginning of the line, do the following:
  • Find 2 digits
  • Follow the instructions -- either .. or __
  • Repeat steps 2 and 3 as many times as possible
  • Make sure it ends with two digits.
+3
source
 ^\d\d((?:\.\.|--|__)\d\d)+$ 

Here is your regex.

+2
source

Tested on Regexpal.com:

(\d{2}(\.{2}|-{2}|_{2}))+\d{2}

This will not allow numbers such as 01.-02

+2
source

All Articles