For Java:

Quantifiers documentation

X, exactly n times: X{n}
X, at least n times: X{n,}
X, at least n but not more than m times: X{n,m}

Answer from shookster on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch02s12.html
2.12. Repeat Part of the Regex a Certain Number of Times - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - 2.12. Repeat Part of the Regex a Certain Number of TimesProblemCreate regular expressions that match the following kinds of numbers:A googol (a decimal number with 100... - Selection from Regular Expressions Cookbook, 2nd Edition [Book]
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
Discussions

regular expression - How to search for any repeating character, X number of times - Vi and Vim Stack Exchange
Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times? I know I can search for .., but those to characters w... More on vi.stackexchange.com
🌐 vi.stackexchange.com
How to find a pattern that repeats up to N times (but not more than N times)?
I guess the answer to this is going to depend on what exactly you are trying to do. In its simplest form, something like this would, in theory, do what you are asking: (?:ab){1,4} However, that may also give you unexpected results with a string like: ababababababab It would match the first four and then the last three. But if you were just checking to see if that was a match, then you'd get a positive, even though you had seven instances of your pattern. You can try adding in some anchors to match the entire string and that would be fine, as long as there's not other text that could appear around your string. ^(?:ab){1,4}$ If, you do have spaces around your string, then you can try adding in word boundaries instead of anchors. \b(?:ab){1,4}\b Here is a demo More on reddit.com
🌐 r/regex
7
6
October 11, 2019
Regex to find a pattern repeating at least n times - Stack Overflow
So I'm trying to put a filter on ... will then repeat at least 3 times. I wrote the following regular expression but on regex101.com,I keep getting a "timeout" message, probably because there is a better way to write it: ... I tried google but my search terms never returned what ... More on stackoverflow.com
🌐 stackoverflow.com
Regex: Repeat pattern n times, with variation on final repetition
I have a regular expression which is intended to match a specific syntax, n times, with a pipe (|) following each occurrence, except for the final occurrence. Currently, my pattern is along the lines of (pattern)\|{3}, but this doesn't satisfy the requirement that there is no trailing pipe. Is there anyway I can accomplish this without repeating ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Upskilld
upskilld.com › learn › matching-a-certain-number-of-repetitions-with-regex
Matching a Certain Number of Repetitions with Regex - Upskilld
We can specify the number of times a particular pattern should be repeated. For example, we want a field to contain an exact number of characters. Other times, we may with to match a number of repetitions in a given range/interval – for example, ensuring that a phone number is between 7 and ...
🌐
Regular-Expressions.info
regular-expressions.info › repeat.html
Regex Tutorial: Repetition with Star and Plus
So our regex will match a tag like <B>. When matching <HTML>, the first character class will match H. The star will cause the second character class to be repeated three times, matching T, M and L with each step. <[A-Za-z0-9]+> also matches valid HTML tags. But it also matches things like <1> that are not valid HTML tag.
🌐
The Carpentries
carpentries-incubator.github.io › regex-novice-biology › 04-repeats › index.html
Repeated Matches – Regular Expressions for Biologists
December 8, 2023 - will match ‘find’, ‘found’, and ‘fiuoaaend’. Note that the whole class can be repeated, and it is not only repeats of the same character that match i.e. the regex ... will match ‘deformed’, as well as ‘dmed’, ‘doomed’, and ‘doooooooooooomed’. It is also important to be aware that the modifiers are ‘greedy’, which means that the regex engine will match as many characters as possible before moving on to the next character of the pattern. a) Which of the follow strings will be matched by the regular expression MVIA*CP?
Find elsewhere
🌐
CodinGame
codingame.com › playgrounds › 218 › regular-expressions-basics › repetitions
Repetitions - Regular Expressions Basics
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
🌐
Araxis
araxis.com › merge › documentation-os-x › regular-expression-reference.en
Regular Expression Reference
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by ‘*’ can be repeated any number of times, including zero.
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 14_regex › spec_sym_repetition.html
Repeating characters - Python for network engineers
Another example of an expression: \d+\s+\S+ - describes a string which has digits first, then whitespace characters, and then non-whitespace characters (all except space, tab, and other similar characters). Using it you can get VLAN and MAC address from string: In [7]: line = '1500 aab1.a1a1.a5d3 FastEthernet0/1' In [8]: re.search('\d+\s+\S+', line).group() Out[8]: '1500 aab1.a1a1.a5d3' Asterisk indicates that the previous expression can be repeated 0 or more times.
🌐
RegexOne
regexone.com › lesson › repeating_characters
RegexOne - Learn Regular Expressions - Lesson 6: Catching some zzz's
A more convenient way is to specify how many repetitions of each character we want using the curly braces notation. For example, a{3} will match the a character exactly three times. Certain regular expression engines will even allow you to specify a range for this repetition such that a{1,3} will match the a character no more than 3 times, but no less than once for example.
🌐
CodeGenes
codegenes.net › blog › how-do-i-repeat-a-character-n-times-in-a-string-used-inside-a-regular-expression-substitution
How to Repeat a Character N Times in a String for Regular Expression Substitution (Perl Example) — codegenes.net
Perl, renowned for its robust regex support, offers elegant solutions to this problem. In this blog, we’ll explore how to use Perl’s regex substitution (s///) combined with string repetition techniques to repeat a character N times.
Top answer
1 of 5
8

The full pcre that will match the strings you listed (and those that start with a ,) might be:

grep -P '^([0-9]+(-[0-9]+)?(,|$))+$'

How have we got there?

The most basic element to match is a digit, lets assume that [0-9], or the simpler \d in PCRE, is a correct regex for a English (ASCII) digit. Which might as well not be. It could match Devanagari numerals, for example. Then you would need to write: [0123456789] to be precise.

Then, a run of digits would be matched by [0-9]+.

After a number (1 or 3 or 26) ther could be a dash '-' followed by one or several digits ( a number again ):

[0-9]+(-[0-9]+)?

Where the ? makes the dash-number sequence optional.

Then, each of those numbers: 3 (or number ranges: 4-9) should be followed by a comma , (several times):

([0-9]+(-[0-9]+)?,)+

Except that the last comma might be missing:

([0-9]+(-[0-9]+)?(,|$))+

And, if required, a leading comma might be present:

(^|,)([0-9]+(-[0-9]+)?(,|$))+

It is a very good idea to anchor the regex to the beginning and end of the text tested:

^((^|,)([0-9]+(-[0-9]+)?(,|$))+)$

You may test and edit the PCRE regex in this site

If the leading comma should be rejected, use:

^(([0-9]+(-[0-9]+)?(,|$))+)$

That leaves no optional interpretations to the regex machine. All must be matched, and anything that is not matched gets rejected.

It may be written as an (GNU) extended regex:

grep -E '^(([0-9]+(-[0-9]+)?(,|$))+)$'

As a Basic Regular Expression (BRE):

grep '^\(\([0-9]\{1,\}\(-[0-9]\{1,\}\)\{0,1\},\{0,1\}\)\{1,\}\)$'

Where the comma , is optional {0,1}, the regex engine might take some decisions about what to match.


Descriptive Regex?

A more descriptive regex, with spaces and comments might be had by starting it with (?x) in pcregrep

pcregrep '(?x)                  # tell the regex engine to allow
                                 # white space and comments.
           (?(DEFINE)            # subroutines that will be used. 
             (?<nrun> [0-9]+)    # run of digits (n-run).

              # define a range pair. A number run followed by
              # an optional ( dash and another number run )
             (?<range> (?&nrun)  (-(?&nrun))? )    # range pair.
             
             (?<sep> ,)          # separator used.
           )                     # end of definitions.

         # Actual regex to use:
         # (range) that ends in a (sep) 
         # or is at the end of the line,
         # several times (+).

         ^(  (?&range)  ((?&sep)|$)  )+$

        ' file

This regex (once compiled) is exactly equivalent to the original one and will run equally fast. Of course, there is an (negligible) additional time used to compile the regex.

Test example is here

2 of 5
5

Using awk to break down each line into comma-delimited fields, and then splitting those fields on dashes into sub-fields, while discarding lines that contains unwanted fields or sub-fields:

BEGIN { FS = "," }

{
    for (i = 1; i <= NF; ++i) {
        # Only the 1st field is allowed to be
        # empty, but only if there are further
        # fields (avoids empty lines).

        if ($i == "" && (i != 1 || NF == 1)) next

        # If the field is split on dashes, it
        # should split into no more than two
        # elements.

        if ((n = split($i, a, "-")) > 2) next

        # Each split-up element needs to be made
        # up of decimal digits only.

        for (j = 1; j <= n; ++j)
            if (a[j] !~ "^[[:digit:]]+$") next
    }

    # The current line is ok to print.

    print
}

This would be used like

awk -f script file

where script holds the awk program.

Or, as "once-liner":

awk -F, '{for(i=1;i<=NF;++i){if(($i==""&&(i!=1||NF==1))||((n=split($i,a,"-"))>2))next;for(j=1;j<=n;++j)if(a[j]!~"^[[:digit:]]+$")next}};1' file

You could easily add a check for "backward ranges" (e.g. 5-2) after the j loop:

if (n == 2 && a[1] > a[2]) next