Hi,
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
- “temp”
- “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.
Thanks,
Amber