Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I match a repeated character or word with regex quantifiers?

Quick answer

A regex quantifier applies to the item immediately before it: ? means zero or one, * means zero or more, + means one or more, and {n} means exactly n repetitions. To repeat a multi-character word such as ha, group it first: (ha){2}. These meanings and grouping rules are documented for Python’s re module and JavaScript regexes. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want to match a letter that may or may not appear, or a letter that repeats. What do ?, *, and + mean?

Geez

Each quantifier applies to the immediately preceding regex item. For the letter a:

  • a? matches zero or one a. It can match an empty string or a.
  • a* matches zero or more adjacent a characters. That includes no a, one a, or several, such as aaaa.
  • a+ matches one or more adjacent a characters. It requires at least one, so it can match a or aaaa, but not an empty string.

Python’s standard-library re documentation and the JavaScript guide describe these meanings. [S1] [S2]

A key detail: a+ repeats only the a; it does not repeat a whole word or longer sequence unless you group that sequence.

Follow-up question

How do I repeat a whole word, like ha, twice? Does putting the quantifier after the word work?

Geez

Group the complete piece you want repeated, then put the quantifier after the closing parenthesis. For example, (ha){2} means the group ha appears exactly twice, so it matches haha. Without grouping, ha{2} repeats only the immediately preceding a; it means h followed by two a characters, so it matches haa, not haha. Both references explain this grouping rule. [S1] [S2]

The same idea works for a longer pattern: put parentheses around the entire sequence that should repeat. The quantifier after the closing parenthesis applies to that grouped sequence, rather than just its final character.

Follow-up question

Is the syntax different in Python and JavaScript? Can you show a small reference I can reuse?

Geez

For the quantifiers covered here, the notation is the same in Python’s standard-library re module and JavaScript regular expressions: ?, *, +, and {n}. Both references document those meanings and the need to group a multi-character pattern before repeating it. [S1] [S2]

Here are examples to use in either language:

  • Optional a: a? — an empty string or a.
  • Zero or more a characters: a* — an empty string, a, or aaaa.
  • One or more a characters: a+ — a or aaaa, but not an empty string.
  • Exactly three a characters: a{3} — aaa.
  • Exactly two ha groups: (ha){2} — haha.

These examples illustrate the documented quantifier and grouping rules. [S1] [S2] They describe what the pattern can match; whether other text is allowed before or after a match depends on how the pattern is used.

Follow-up question

What’s a reliable way to build one without accidentally repeating the wrong part?

Geez

First identify the exact character or sequence that should repeat. If it has multiple characters, put parentheses around the whole sequence, then append the quantifier. Check what is immediately before the quantifier: that is the item it applies to. [S1] [S2]

For example, if the goal is exactly two ha groups, write (ha){2}. It matches haha. The similar-looking ha{2} is different: its quantifier applies only to a, so it matches haa. [S1] [S2]

You can also use a quantifier after a group to specify a different repetition rule: (ha)* allows zero or more copies of the group, while (ha)+ requires one or more. Choose based on whether the whole group is optional or must appear at least once. [S1] [S2]

A practical checklist

  • Write down the complete character or sequence you want to repeat.

  • Choose ? for zero or one, * for zero or more, + for one or more, or {n} for exactly n repetitions.

  • If the piece has multiple characters, group it before adding the quantifier.

  • Check which item is immediately before the quantifier; that is what it repeats.

  • Compare a clear matching example with a near-miss example.

A few more useful answers

What is the difference between `ha{2}` and `(ha){2}`?

ha{2} repeats only a, so it matches haa. (ha){2} repeats the whole group, so it matches haha. [S1] [S2]

Does `a*` require at least one `a`?

No. The * quantifier allows zero or more, so it can match no a at all. Use a+ when at least one a is required. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Python 3.15 Regular Expression Operations: repetition operators
    docs.python.org · Checked
  2. [S2] MDN JavaScript Guide: quantifiers and repeated patterns
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work