Regex exactly n OR m times

Consider the following regular expression, where X is any regular expression.

 X{n}|X{m} 

This regular expression will check on X exactly n or m times.

Is there a regular expression quantifier that can check for the presence of X exactly n or m times?

+73
java php regex
Dec 14
source share
7 answers

There is no single quantifier that means "exactly m or n times." The way you do it is wonderful.

Alternative is

 X{m}(X{k})? 

where m < n and k is the value of nm .

+66
Dec 14 '12 at 8:10
source share

Here is a complete list of quantifiers (link http://www.regular-expressions.info/reference.html ):

  • ? , ?? - 0 or 1 occurrences ( ?? lazy ? Is greedy)
  • * , *? - any number of inputs
  • + , +? - at least one appearance
  • {n} - exactly n occurrences
  • {n,m} - n to m occurrences, inclusive
  • {n,m}? - n to m occurrences, lazy
  • {n,} , {n,}? - at least n occurrences

To get "exactly N or M", you need to write a quantified regular expression twice, unless m, n are special:

  • X{n,m} if m = n+1
  • (?:X{n}){1,2} if m = 2n
  • ...
+41
Dec 14 '12 at 8:16
source share

No, there is no such quantifier. But I restructured it to /X{m}(X{mn})?/ To prevent problems in the opposite direction .

+18
Dec 14 '12 at 8:12
source share

TL; DR; (?<=[^x]|^)(x{n}|x{m})(?:[^x]|$)

It looks like you want to "xn times" or "xm times", I think the literal translation in regex will be (x{n}|x{m}). Like https://regex101.com/r/vH7yL5/1

or, in case you can have a sequence greater than m "x" s (assuming m> n), you can add 'follow no "x"' and 'follow after no "x", translating to [^x](x{n}|x{m})[^x] , but this assumes that there is always a character and "x" after you. As you can see here: https://regex101.com/r/bB2vH2/1

you can change it to (?:[^x]|^)(x{n}|x{m})(?:[^x]|$) by translating to "follow no" x "or after the beginning of the line" and "followed by" x "or followed by the end of the line." But still, it will not correspond to two sequences with one symbol between them (because for the first match you will need a symbol after, and the second - a symbol earlier), as you can see here: <a2>

Finally, to match one character deletion, you can add a positive look ahead (? =) To no x after or a positive look (? <=) To no x 'before, for example: https: / /regex101.com/r/mC4uX3/1

 (?<=[^x]|^)(x{n}|x{m})(?:[^x]|$) 

This way you will only match the exact number "x you want".

+1
Jul 07 '16 at 16:03
source share

Looking at the extended answer, they argue that their penultimate expression will not match single-character sequences between them. There is an easy way to fix this without using a forward / backward look and replace the start / end character with a border character. This allows you to match word boundaries, including start / end. Thus, the corresponding expression should be:

(?:[^x]|\b)(x{n}|x{m})(?:[^x]|\b)

As you can see here: https://regex101.com/r/oC5oJ4/2 .

0
May 08 '18 at 12:13
source share

A very old post, but I would like to make a contribution that could help. I tried exactly as indicated in the question, and it works, but there is one catch: the order of quantities matters. Consider this:

 #[a-f0-9]{6}|#[a-f0-9]{3} 

This will allow you to find all cases of using hexadecimal color codes (3 or 6 digits long). But when I flip it like that

 #[a-f0-9]{3}|#[a-f0-9]{6} 

he will find only 3 digits or the first 3 digits of 6 digits. It makes sense, and a Regex professional can immediately notice it, but for many, it can be a kind of behavior. There are some advanced Regex features that can escape this trap regardless of order, but not all knee-deep in Regex templates.

0
Jan 23 '19 at 14:02
source share

You can also write it hard, (X{n})|(X{m})

-3
Dec 14
source share



All Articles