-2

Can anyone please explain to me how this works in a python regular expression pattern? I tried to google but it didn't help:

a{1}
a{1, }
a{2, 3}
a{0, 1}
a{0, }
Gino Mempin
  • 12,644
  • 19
  • 50
  • 69
MiiRana
  • 13
  • 3
  • This doesn't look like anything close to valid Python, unless perhaps it's part of a regular expression. Where did you see it? Perhaps you could give us a URL? – Karl Knechtel Sep 26 '20 at 11:01
  • In python, this statement shoudn't work at all. Maybe are you confusing with `a = {}` that creates a dictionary? – j3r3mias Sep 26 '20 at 11:02
  • yeah it is a part of a regular expression, but this all I have found in a book in french: can you explain to me how can I use it a{1} répète le caractère précédent 1 fois: a # a{1, } répète le caractère précédent au moins 1 fois: a,aa, aaa … # a{2, 3} répète le caractère précédent entre m et n fois:aa, aaa – MiiRana Sep 26 '20 at 11:05
  • 4
    @MiiRana Can you please edit the question with that information. –  Sep 26 '20 at 11:11
  • Does this answer your question? [What do comma separated numbers in curly braces at the end of a regex mean?](https://stackoverflow.com/questions/17032914/what-do-comma-separated-numbers-in-curly-braces-at-the-end-of-a-regex-mean) – Gino Mempin Sep 26 '20 at 14:42

1 Answers1

2

It's called "repetition qualifier" and it applies to preceding character (e.g. a), set of characters (e.g. [0-9]) or group (e.g. (foo)) in regexp and with a single digit argument {n} means exactly n times. With two arguments {m, n} it means at least m and no more then n times is expected to repeat in order to match. When the upper bound (n,) is missing ({m,}), it means at least m and any greater number of repetitions. Omitting first argument ({,n}) uses lower bound of 0 (no repetition). You can also check out the docs.

  • a{1} : exactly one a (same as just a)
  • a{1, }: one or more occurrences of a (a+)
  • a{2, 3}: two or three occurrences of a
  • a{0, 1}: one or none occurrence of a (a?)
  • a{0, }: zero or more (any number of) occurrence of a (a*)
Ondrej K.
  • 7,255
  • 11
  • 17
  • 29
  • Since this got more attention either way than it needed, a helpful link: https://meta.stackoverflow.com/questions/255459/is-it-okay-to-downvote-answers-to-bad-questions Also, not to mention the overall scoring and status of the question was different (positive) at the time of answering, generally judging the answer by association and trying to "teach a lesson" into not trying to help by essentially flouting the community rules is not exactly model behavior and setting a good example. – Ondrej K. Sep 27 '20 at 13:44