Python regex library is not returning the result properly


I first time use the regex library in python and basically, I am using the regex library in C++17 also and that is giving me the expected result as an output of the same function. I am providing the example below:

Python implementation:

import re
def CheckUsername(name):
    LOGIN_NAME_MAX = 128
    if not name or len(name) > LOGIN_NAME_MAX:
        return False

    pattern = "^[a-zA-Z_][a-zA-Z0-9_-]*[$]?"
    return bool(re.match(pattern, name))

C++ implementation:

#include <regex>
static bool
CheckUsername(const string& name)
   if (name.empty() || name.length() > LOGIN_NAME_MAX) {
      return false;
   // NOTE: This regex was suggested in the useradd man pages
   regex re("^[a-zA-Z_][a-zA-Z0-9_-]*[$]?");
   return regex_match(name, re);

Test Input

  1. “temp”
  2. “temp@”

Expected result in both Python and C++

print(CheckUsername("temp")) # Output: True
print(CheckUsername("test@")) # Output: False

As I told you earlier the “test@” is returning me “True” in python but the expected result should be “False”. I think there is an issue somewhere.


I think you want to use re.fullmatch or terminate the match with $:

1 Like

This is the standard username validation regex so I took this from a man page. So the C++ function is giving me the expected result but not the python. I added in the expected result if both the test inputs are able to provide the expected output (written next to the function as a comment) then we are good to go.

re.match() tests whether the regular expression matches the beginning of the string. Use re.fullmatch() to test whether it matches the whole string.

^ at the beginning of the regular expression used with re.match() or re.fullmatch() is redundant.

I wonder whether you intentionally accept names like “test$” as a username?

1 Like

Hello Serhiy,
Thanks for giving me the idea of fullmatch. Actually, i was directly converting C++ regex.match() to python re.match(). Now, i am getting the exact result after substituting fullmatch in place of match.

Thank you so much team.