Validation
Lowercase Letters Only Regex
Identifiers such as language codes, tag names, environment names and some command names are conventionally written in lowercase letters only. This pattern accepts one or more characters from a to z and nothing else, so a capital letter, a digit, a space or any symbol makes the input fail. Because the rule is strict and simple, it is easy to explain in an error message: use lowercase letters only. If you would rather accept mixed case and normalise it, call toLowerCase() before saving and use a letters-only check instead. Add digits or hyphens to the class when your identifiers need them.
Pattern (JavaScript)
/^[a-z]+$/Starts at the start of the line/string, then a lowercase letter, one or more times, then the end of the line/string.
Examples
Matches
- ✓ hello
- ✓ abc
- ✓ regexlab
Does not match
- ✗ Hello
- ✗ abc1
- ✗ two words
Try it
0 matches (JavaScript engine)
hello
abc
regexlab
Hello
abc1
two wordsCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[a-z]+$/g;
const text = "hello";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[a-z]+$')
text = 'hello'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[a-z]+$/';
$text = 'hello';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-z]+$");
String text = "hello";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-z]+$`)
text := "hello"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[a-z]+$");
var text = "hello";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[a-z]+$").unwrap();
let text = "hello";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[a-z]+$/
text = 'hello'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why not just lowercase the value automatically?
- Automatic conversion is often better for users. This pattern is useful where the stored value must already be lowercase, such as config keys checked by a linter.
- How do I allow lowercase letters with digits?
- Use ^[a-z0-9]+$. If the first character must be a letter, use ^[a-z][a-z0-9]*$.
- Does the i flag break this pattern?
- Yes. With the i flag, [a-z] also matches capital letters, which defeats the purpose. Keep the flags empty.