4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to match letters with Regular Expressions?

By 4Geeks

Match any letter, uppercase or lowercase, check that a text has only letters, handle accents and reuse ready-made regex examples.

7 min readLast updated
Regex for letters icon

What is the regex for letters?

Short answer: [a-zA-Z] matches any single letter, uppercase or lowercase. Add + to match whole words of letters, and wrap it in ^...$ to check that a text has only letters:

[a-zA-Z]        one letter
[a-zA-Z]+       one or more letters in a row
^[a-zA-Z]+$     the whole text is letters
[A-Z]           one uppercase letter
[a-z]           one lowercase letter

[a-zA-Z] only covers the English alphabet. For letters with accents, such as é or ñ, use [^\W\d_] in Python or \p{L} with the u flag in JavaScript.

[a-zA-Z]+ finds English letters, fullmatch() checks the whole text and [^\W\d_]+ also keeps letters with accents.

Regex for letters cheat sheet

What you wantRegexMatches in "Hello World 123"
Any letter[a-zA-Z]H, e, l...
Words made of letters[a-zA-Z]+Hello, World
Uppercase letters[A-Z]H, W
Lowercase letters[a-z]+ello, orld
Only letters, whole text^[a-zA-Z]+$No match: it has spaces and digits
Anything that is not a letter[^a-zA-Z]The spaces and 1, 2, 3
Words that start with a capital\b[A-Z]\w*Hello, World
Any letter in any language (Python)[^\W\d_]Also é, ü, ñ
Any letter in any language (JavaScript)/\p{L}/uAlso é, ü, ñ

Match letters in Python

Python uses the re module. Write patterns as raw strings (r"...") so backslashes stay as they are:

python
import re
 
text = "Hello World 123"
 
print(re.findall(r"[a-zA-Z]+", text))   # ['Hello', 'World']
print(re.findall(r"[A-Z]", text))       # ['H', 'W']
 
print(bool(re.fullmatch(r"[a-zA-Z]+", "Hello")))   # True
print(bool(re.fullmatch(r"[a-zA-Z]+", "Hello1")))  # False

re.findall() returns every match in a list, and re.fullmatch() checks that the whole text follows the pattern. To ignore case, use [a-z] with flags=re.I.

Match letters in JavaScript

javascript
const text = "Hello World 123";
 
console.log(text.match(/[a-zA-Z]+/g));      // ["Hello", "World"]
console.log(/^[a-zA-Z]+$/.test("Hello"));    // true
console.log(/^[a-zA-Z]+$/.test("Hello1"));   // false
console.log("Hello World".match(/[a-z]+/gi)); // ["Hello", "World"]

The g flag returns every match, not only the first one, and i ignores case. .test() returns true or false.

Career · AI Engineering for Developers

You can match any pattern, now build AI apps with it

Use the coding skills you already have to build AI agents, RAG systems and LLM-powered apps, with mentors and real projects.

Letters with accents and other alphabets

[a-zA-Z] doesn't include é, ñ, ü or letters from other alphabets, so it cuts names in pieces:

python
import re
 
print(re.findall(r"[a-zA-Z]+", "José Müller"))   # ['Jos', 'M', 'ller']
print(re.findall(r"[^\W\d_]+", "José Müller"))   # ['José', 'Müller']

[^\W\d_] means "a word character that is not a digit or an underscore", which in Python 3 is any Unicode letter. Python's re module doesn't support \p{L}: it raises re.error: bad escape \p. If you need it, install the third-party regex module.

In JavaScript, use \p{L} with the u flag:

javascript
console.log("José Müller 42".match(/\p{L}+/gu));  // ["José", "Müller"]

Without the u flag, \p{L} doesn't work and the match returns null.

Find words by their letters

What you wantRegexExample match
Words that start with B or b\b[bB]\w*Bob, bought, blue
Words that end in s\w*s\bcats, dogs
Words that start with a capital\b[A-Z]\w*This, Is, Example
Remove everything except lettersre.sub(r"[^a-zA-Z]", "", text)"R2-D2" becomes "RD"
python
import re
 
print(re.findall(r"\b[bB]\w*", "Bob bought blue balloons"))  # ['Bob', 'bought', 'blue', 'balloons']
print(re.findall(r"\w*s\b", "cats and dogs run fast"))      # ['cats', 'dogs']
print(re.sub(r"[^a-zA-Z]", "", "R2-D2 & C-3PO!"))            # RDCPO

\b is a word boundary: the point where a word starts or ends.

More regex examples: emails, numbers, passwords and URLs

These patterns are a good starting point for common validations. Test them with your own data before you use them.

What you checkRegexValidNot valid
Email^[\w.+-]+@[\w-]+\.[\w.-]+$ana.lopez+news@mail.co.ukana@, ana example.com
Integer^-?\d+$15, -712.3, 56,7
Decimal number^-?\d+\.\d+$12.3, -0.515, 56,7
Password: 8+ characters, upper, lower and digit^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$Geek2026xgeek2026x, Short1A
URL with http or https^https?://\S+$https://4geeks.comftp://x.com
python
import re
 
password = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$"
print(bool(re.match(password, "Geek2026x")))  # True
print(bool(re.match(password, "geek2026x")))  # False, no uppercase letter

To check an image file name, use \.(jpg|png|svg)$: it matches photo.jpg and logo.svg, but not doc.pdf.

The (?=...) parts are lookaheads: each one checks that the text contains something, without consuming it. A regex can't fully validate an email address; to be sure, send a confirmation email.

Use AI to write and explain regex

An assistant like ChatGPT, Claude or GitHub Copilot is very good at regex, but it also makes mistakes, so always test the result. These three prompts are ready to copy.

1. Write a pattern

Write a regex for [Python / JavaScript] that matches [describe what you want],
for example [valid examples], but not [invalid examples]. Explain each part.

2. Explain a pattern

Explain this regex step by step in simple words and give me 3 texts that match
and 3 that don't: [paste the regex]

3. Fix a pattern

This regex [paste it] should match [what you expect] but it [what happens instead]
with this text: [paste the text]. Find the problem and fix it.

Test every pattern with real examples before you use it in your code.

Learn with a mentor

From regex patterns to shipping AI features

In the 4Geeks AI Engineering for Developers program you add AI agents, RAG and production LLM apps to your portfolio.

Common problems with regex for letters

What you seeWhy it happensFix
José becomes Jos[a-zA-Z] only covers English lettersUse [^\W\d_] in Python or /\p{L}/gu in JavaScript
^[a-zA-Z]+$ fails with "Ana Lopez"The space is not a letterAllow it: ^[a-zA-Z ]+$
JavaScript only returns the first matchThe g flag is missingAdd it: /[a-zA-Z]+/g
/\p{L}/ returns nullThe u flag is missingUse /\p{L}/u
re.error: bad escape \pPython's re doesn't support \p{L}Use [^\W\d_], or the regex module
\w also matches digits and _\w means letters, digits and underscoreUse [a-zA-Z] or [^\W\d_]
[A-z] matches [, ^ and _The range includes symbols between Z and aWrite [A-Za-z]
Free download

Regex for letters in a 2-page cheat sheet

Patterns for letters in Python and JavaScript, accents and Unicode, ready-made regex for emails, numbers, passwords and URLs, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Regex for letters cheat sheet

2 pages · PDF · 4Geeks

  • Every letter pattern with real matches
  • Python and JavaScript side by side
  • Ready-made patterns and prompts to write your own with AI

Get it free

Your PDF right away

Frequently asked questions about regex for letters

Want to go from regex and string handling to building AI apps, with real projects and mentors?

See the AI Engineering for Developers program