Hone

Lessons · Regex · +: one or more

One or more

+ means one or more of the token before it. a+ matches a, aa, aaa, and never an empty run.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Words, numbers, runs of spaces: real text comes in runs, and + is how you say 'at least one, as many as there are'.

How to think about it

Attach + to a single token or a group: \d+ for a number, [a-z]+ for a word, (ab)+ for repeated pairs. If zero of them is also fine, that is *, not +.

Worked example

pattern: \d+
One or more digits.
text: 2026 and 7
Two matches: 2026, then 7.
pattern: ab+
a followed by one or more b.
text: a ab abbb
Matches ab and abbb; the lone a needs at least one b.

Your turn

A word of one or more letters.

[a-zA-Z]

The trap

+ is greedy: \d+ takes the whole run. On '2026' it never matches 2 alone, which surprises people expecting the shortest match.

Practise +: one or more on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.