Two alternatives: the very long and very short versions. Both assumes that
RUC (without check digit) has a length of 6 to 8 characters.
- The very long version:
Here, the only alphanumeric character that could appear at the end of the document is the capital A.
^(?:(?!0)\d{6,8}|(?!0)\d{5,7}A)(?:-\d)?$
^ # Start of string
(?: # Start of first non-capturing group:
(?!0) # Negative lookahead (?!): avoid starting with zero
\d{6,8} # Digits (an equivalent is [0-9]); 6 to 8 times
| # Or
(?!0) # Negative lookahead (?!): avoid starting with zero
\d{5,7}A # Digits (an equivalent is [0-9]); 5 to 7 times, followed by an 'A'
) # End of first non-capturing group
(?: # Start of second non-capturing group:
-\d # A hyphen followed by a single digit
)? # End of second non-capturing group; '?' = 'optional': may or may not appear
$ # End of string
Version with capturing groups:
^((?!0)\d{6,8}|(?!0)\d{5,7}A)(-\d)?$
- The very short version:
In this case, only capital A or B or C or D can be the alphanumeric characters that could appear at the end of the document.
^[1-9]\d{4,6}[\dA-D](?:-\d)?$
# or
^[1-9]\d{4,6}[\dA-D](-\d)?$
^ # Start of string
[1-9] # Digits from 1 to 9 (avoid starting with zero)
\d{4,6} # Digits (an equivalent is [0-9]); 4 to 6 times
[\dA-D] # Digits or capital A/B/C/D
(?: # Start of non-capturing group:
-\d # A hyphen followed by a single digit
)? # End of second non-capturing group; '?' = 'optional': may or may not appear
$ # End of string
Both tested in Javascript.
Fact: The letter "A" was added to the document numbers that were mistakenly duplicated.