Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Grouping, Quantifiers, and Anchors | Intermediate Regular Expressions
Python Regular Expressions

bookGrouping, Quantifiers, and Anchors

Understanding how to group parts of a pattern, specify the number of repetitions, and define where a match should occur within a string are essential skills for writing powerful regular expressions. In Python, you use parentheses () to create groups, which let you treat multiple characters as a single unit. Quantifiers like *, +, ?, and {n,m} control how many times a character or group should appear. Anchors such as ^ and $ are special symbols that match the start or end of a string, respectively.

Grouping with Parentheses

Grouping with parentheses allows you to apply quantifiers to entire sub-patterns rather than individual characters. For example, (ab)+ matches one or more repetitions of the sequence ab.

Quantifiers

  • The asterisk (*) means "zero or more times";
  • The plus sign (+) means "one or more times";
  • The question mark (?) means "zero or one time";
  • Curly braces ({n,m}) specify a minimum (n) and maximum (m) number of times.

Anchors

Anchors are not characters to be matched, but rather markers for positions in the string. The caret (^) asserts the start, while the dollar sign ($) asserts the end of a string.

1234567
import re text = "hello hello world world world" pattern = r"(world\s+)+" matches = re.findall(pattern, text) print(matches)
copy

Step-by-Step Analysis: Grouping and Quantifiers

In this example, the regular expression pattern (world\s+)+ is used to find repeated occurrences of the word world followed by spaces. Let's break down how this pattern works:

  • The parentheses () create a group around world\s+, treating the word world and the following whitespace as a single unit;
  • The plus sign + quantifier directly after the group means that this group must appear one or more times in a row;
  • As a result, the pattern matches every sequence where world is repeated with spaces in between, capturing the longest such sequence.

Grouping is especially useful when you want to apply quantifiers to a sequence of characters or to capture parts of a match for later use. Quantifiers can be applied to single characters, character classes, or entire groups. Using anchors with these patterns lets you further control where your matches occur, such as matching a repeated word only at the beginning or end of a string.

question mark

What does the regex pattern (ab)+ match?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain why the output is only one match instead of multiple?

How would the result change if I used a different quantifier, like `*` or `{2,}`?

Can you show how to use anchors with this pattern to match only at the start or end of the string?

Awesome!

Completion rate improved to 6.67

bookGrouping, Quantifiers, and Anchors

Veeg om het menu te tonen

Understanding how to group parts of a pattern, specify the number of repetitions, and define where a match should occur within a string are essential skills for writing powerful regular expressions. In Python, you use parentheses () to create groups, which let you treat multiple characters as a single unit. Quantifiers like *, +, ?, and {n,m} control how many times a character or group should appear. Anchors such as ^ and $ are special symbols that match the start or end of a string, respectively.

Grouping with Parentheses

Grouping with parentheses allows you to apply quantifiers to entire sub-patterns rather than individual characters. For example, (ab)+ matches one or more repetitions of the sequence ab.

Quantifiers

  • The asterisk (*) means "zero or more times";
  • The plus sign (+) means "one or more times";
  • The question mark (?) means "zero or one time";
  • Curly braces ({n,m}) specify a minimum (n) and maximum (m) number of times.

Anchors

Anchors are not characters to be matched, but rather markers for positions in the string. The caret (^) asserts the start, while the dollar sign ($) asserts the end of a string.

1234567
import re text = "hello hello world world world" pattern = r"(world\s+)+" matches = re.findall(pattern, text) print(matches)
copy

Step-by-Step Analysis: Grouping and Quantifiers

In this example, the regular expression pattern (world\s+)+ is used to find repeated occurrences of the word world followed by spaces. Let's break down how this pattern works:

  • The parentheses () create a group around world\s+, treating the word world and the following whitespace as a single unit;
  • The plus sign + quantifier directly after the group means that this group must appear one or more times in a row;
  • As a result, the pattern matches every sequence where world is repeated with spaces in between, capturing the longest such sequence.

Grouping is especially useful when you want to apply quantifiers to a sequence of characters or to capture parts of a match for later use. Quantifiers can be applied to single characters, character classes, or entire groups. Using anchors with these patterns lets you further control where your matches occur, such as matching a repeated word only at the beginning or end of a string.

question mark

What does the regex pattern (ab)+ match?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt