Hone

Lessons · Regex · any one character

Any one character

The dot matches exactly one character of any kind, except a newline.

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

Wildcards in names and codes, 'a then something then b', skipping a character you do not care about.

How to think about it

Is exactly one unknown character allowed here? Use a dot for that spot. For 'anything of any length' it is .* and for 'anything but X' a negated class is safer than a dot.

Worked example

pattern: ^h.t$
h, any one character, t.
text: hat
Match. hot and hit too.
text: heat
No match: two characters in the middle.

Your turn

Match 'c', any character, 'p' as a whole string.

^cp$

The trap

A dot where you meant a literal full stop. 1.5 as a pattern matches 125. Escape it: 1\.5.

Practise any one character on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.