Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Understanding Curly Braces in Regular Expressions or Regex

Posted on August 12, 2021August 12, 2021 by admin

Overview

Curly braces act as a repetition quantifier in regex. They specify the number of times a character before preceding it can appear in the input string or text. They can also be used to specify a range i.e specify the minimum and maximum of times a  character can appear. 

Its syntax is 

{min, max}

where 

  • min denotes the minimum number of times a character can appear
  • max denotes the maximum number of times a character can appear

For example

a{n}

This specifies that character “a” can appear exactly n times. Similarly for the below regex

\d{n}

This specifies that any digit can appear exactly n times. Curly braces can also be used to define a range.
For example

  • {m,n} – Atleast m and up to n times
  • {m, } – Atleast m times
  • {, n} – Upto n times

Let’s see an example for the same in ruby language

main.ruby

regex = "b{2}"

input = "bb"
match = input.match(/#{regex}/)
puts match

input = "bbb"
match = input.match(/#{regex}/)
puts match

Output

bb
bb

By default, curly braces are greedy or non-lazy. What does it mean? They will match all the possible characters and always prefers more. It is also possible to make the curly braces operator non-greedy or lazy. This can be done by adding a question mark after the curly braces operator. Let’s see an example for the same.

As you can see from the output that after adding a question mark operator after the curly brace operator, it tries to match the minimum number of characters as possible i.e it becomes nongreedy

That is why given regex

ab{2,4}

It gives a match abb for all below input strings

abb
abbb
abbbb

Program for the same in ruby language

main.ruby

regex = "ab{2,4}"

input = "abb"
match = input.match(/#{regex}/)
puts match

input = "abbb"
match = input.match(/#{regex}/)
puts match

input = "abbbb"
match = input.match(/#{regex}/)
puts match

Output

abb
abbb
abbbb

while

ab{2,4}? will always give match as abb for all the above input strings

Program for the same

regex = "ab{2,4}?"

input = "abb"
match = input.match(/#{regex}/)
puts match

input = "abbb"
match = input.match(/#{regex}/)
puts match

input = "abbbb"
match = input.match(/#{regex}/)
puts match

Output

abb
abb
abb

Curly braces applied to a grouping

A part of the regular expression can be placed inside a balanced parenthesis. This part is one group now. We can additionally apply curly braces to this group. The curly braces will be added after the grouping 

Let’s see an example of the same.

regex = "(ab){2}"

input = "ababb"
match = input.match(/#{regex}/)
puts match

input = "ababbc"
match = input.match(/#{regex}/)
puts match

Output

abab
abab

Curly Braces applied to a Character Class

Curly Brace quantifier can also be applied to the entire character class. Its meaning remains the same though. A character class is represented by square brackets in regex. Let’s see a program for the same.

We have below regex in the above program

[ab]{4}

It means it would match a string of length exactly 4 and comprised of characters ‘a’ and ‘b’ in any order

That is why the regex matches the below string

abab
aaaa
bbbb
aabb
bbaa

And it does not match

aba - String of length 3
abbaa - String of length 5

How to use the curly brace as a literal character in regex. 

The escaping character can be placed before the opening brace or closing brace if they are needed to be used in a literal way.

A closing brace that is not preceded by an opening is treated as a literal closing brace. 

That is all about curly braces in regex in Regex. Hope you have liked this article. Please share feedback in the comments

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme