Validation
Yes or No Answer Regex
Command-line tools and chat bots often ask a yes or no question and need to check the reply. This pattern accepts the full words yes and no as well as the single letters y and n. The i flag makes it case-insensitive, so YES, No and Y all pass. Anchors at both ends reject longer replies such as yes please or nope, which avoids guessing what the user meant. After a match, compare the first letter in lower case to decide the answer. Trim the input first, since a trailing space or newline from a terminal would otherwise make the match fail.
Pattern (JavaScript)
/^(yes|no|y|n)$/iStarts at the start of the line/string, then a group (the text "yes" OR the text "no" OR the character "y" OR the character "n"), then the end of the line/string.
Examples
Matches
- ✓ Yes
- ✓ n
- ✓ NO
Does not match
- ✗ maybe
- ✗ yes please
- ✗ nope
Try it
0 matches (JavaScript engine)
Yes
n
NO
maybe
yes please
nopeCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(yes|no|y|n)$/gi;
const text = "Yes";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(yes|no|y|n)$', re.I)
text = 'Yes'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(yes|no|y|n)$/i';
$text = 'Yes';
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("^(yes|no|y|n)$", Pattern.CASE_INSENSITIVE);
String text = "Yes";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)^(yes|no|y|n)$`)
text := "Yes"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(yes|no|y|n)$", RegexOptions.IgnoreCase);
var text = "Yes";
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"(?i)^(yes|no|y|n)$").unwrap();
let text = "Yes";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(yes|no|y|n)$/i
text = 'Yes'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I tell which answer was given?
- After a successful match, check whether the first character in lower case is y. If it is, the answer is yes; otherwise it is no.
- Why does input from the terminal sometimes fail?
- Reading a line often keeps the trailing newline. Trim the input before testing, or the dollar anchor will not match.
- Can I add words like true and false?
- Yes. Extend the alternation to (yes|no|y|n|true|false). Keep the anchors so partial words are still rejected.