Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Regex - Get Started
  • Regex Syntax
  • Characters
  • Metacharacters
  • Quantifiers
  • Character Classes
  • Grouping
  • Lookarounds
  • Substitution
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Quantifiers

In regular expressions, quantifiers are metacharacters that specify how many times the previous character or group should be matched.

Quantifiers allow you to specify the number of occurrences of a character or group, making it easier to match patterns of varying lengths.

However, it's important to use quantifiers wisely because using them incorrectly can lead to the wrong results.

Regex QuantifierDescription
++ indicates that the previous character must occur at least one or more times.
?? indicates that the preceding character is optional. It means the preceding character can occur zero or one time.
*Matches zero or more of the preceding character.
{n}Matches exactly n occurrences of the preceding character.
{n,}Matches n or more occurrences of the preceding character.
{n,m}Matches between n and m occurrences of the preceding element

Greedy vs. lazy matching

Regex quantifiers, such as *, +, and ?, can be greedy or lazy. Greedy matching attempts to match as much as possible, while lazy (or non-greedy) matching matches as little as possible.

Let's see how to use quantifiers in regex pattern.

The following uses the + quantifier.

//g
0

The + sign is used to find one or more occurrences of the previous character. So, ‘colou+r' pattern finds ‘colour' and ‘colouuuur' where ‘u' is present at least once between ‘colo' and ‘r'.

The following uses the ? quantifier:

//g
0

The ? acts as an optional quantifier. The u? in the colou?r pattern makes 'u' as an optional char that may come zero or one time but not more.

The following uses the * quantifier:

//g
0

The * quantifier finds zero or more occurrences of the previous character. The colou*r pattern finds all combinations whether u occurred zero, one, or more times.

The following uses {n} and {n,} quantifiers:

//g
0
//g
0

The {1} in colou{1}r finds exactly 1 occurrence of 'u' in the input string, whereas colou{1,}r finds one or more occurrence of 'u'.

//g
0

The colou{1,4}r finds one to four occurrences of 'u' in the input string.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.