liveBook · Manning

One-or-more quantifier

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A one-or-more quantifier is a regular-expression quantifier requiring the preceding element or group to occur at least once.

The one-or-more quantifier, written +, requires the preceding character class, expression, or group to match at least once. In Python regular expressions, adding + to a consonant class changes a match from the first consonant to a leading consonant sequence: chair produces ch. section 14.1.2

Match repeated characters

>>> re.match(f'[{consonants}]+', 'chair') <re.Match object; span=(0, 2), match='ch'> >>> re.search(r'\d+', 'abc123!') <re.Match object; span=(3, 6), match='123'> >>> re.search(r'\w+', 'abc123!') <re.Match object; span=(0, 6), match='abc123'> Because + requires a match, the consonant pattern returns None for apple. Combining + with capture groups can split a word into repeated leading consonants and the remaining text, while combining it with re.split() groups adjacent non-word characters such as ', ' together. section 14.1.2 section 14.1.3 section 15.1.2 section 15.1.4

Figure 14.4 Adding a plus sign to the class will match one or more characters.

Use the figure to compare a character class without + against the one-or-more form. section 14.1.2

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Defines the quantifier and demonstrates successful, failed, captured, and split matches without examining regex-engine internals.
Examples
Demonstrates consonants in chair, digits in abc123!, word characters, vowel-word groups, and punctuation in a sentence.
What it assumes
Assumes basic regular-expression matching and character classes while introducing repeated matching through concrete Python expressions.
What you can do after
Apply it via Python's re module to require and capture one or more consecutive characters or groups.
How it connects
Connects + with character classes, capture groups, re.match(), re.search(), re.split(), and optional matching.

Also explained in

Compare how 2 books explain one-or-more quantifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.