Home > Mobile >  Why does the regex [a-zA-Z]{5} return true for non-matching string?
Why does the regex [a-zA-Z]{5} return true for non-matching string?

Time:03-02

I defined a regular expression to check if the string only contains alphabetic characters and with length 5:

use regex::Regex;

fn main() {
  let re = Regex::new("[a-zA-Z]{5}").unwrap();
  println!("{}", re.is_match("this-shouldn't-return-true@"));
}

The text I use contains many illegal characters and is longer than 5 characters, so why does this return true?

CodePudding user response:

You have to put it inside ^...$ to match the whole string and not just parts:

use regex::Regex;

fn main() {
    let re = Regex::new("^[a-zA-Z]{5}$").unwrap();
    println!("{}", re.is_match("this-shouldn't-return-true@"));
}

Playground.

As explained in the docs:

Notice the use of the ^ and $ anchors. In this crate, every expression is executed with an implicit .*? at the beginning and end, which allows it to match anywhere in the text. Anchors can be used to ensure that the full text matches an expression.

CodePudding user response:

Your pattern returns true because it matches any consecutive 5 alpha chars, in your case it matches both 'shouldn't' and 'return'.

Change your regex to: ^[a-zA-Z]{5}$

^            start of string
[a-zA-Z]{5}  matches 5 alpha chars
$            end of string

This will match a string only if the string has a length of 5 chars and all of the chars from start to end fall in range a-z and A-Z.

  • Related