使用正则表达式验证字符串不是以特定字符和字符串长度开头

时间:2022-04-15 21:39:13

I have a function with the following if statements:

我有一个带有以下if语句的函数:

if (name.length() < 10 || name.length() > 64)
{
    return false;
}

if (name.front() == L'.' || name.front() == L' ')
{
    return false;
}

I was curious to see if can do this using the following regular expression:

我很想知道是否可以使用以下正则表达式执行此操作:

^(?!\ |\.)([A-Za-z]{10,46}$)

to dissect the above expression the first part ^(?!\ |.) preforms a negative look ahead to assert that it is impossible for the string to start with space or dot(.) and the second part should take care of the string length condition. I wrote the following to test the expression out:

剖析上面的表达式,第一部分^(?!\ |。)预先形成一个负向的表示断言字符串不可能以空格或点(。)开头,第二部分应该处理字符串长度条件。我写了以下内容来测试表达式:

std::string randomStrings [] = {" hello", 
                                " hellllloooo", 
                                "\.hello", 
                                ".zoidbergvddd", 
                                "asdasdsadadasdasdasdadasdsad"};

std::regex txt_regex("^(?!\ |\.)([A-Za-z]{10,46}$)");

for (const auto& str : randomStrings)
{
    std::cout << str << ": " << std::boolalpha << std::regex_match(str, txt_regex) << '\n';
}

I expected the last one to to match since it does not start with space or dot(.) and it meets the length criteria. However, this is what I got:

我期望最后一个匹配,因为它不以空格或点(。)开头,并且它符合长度标准。但是,这就是我得到的:

 hello: false
 hellllloooo: false
.hello: false
.zoidbergvddd: false
asdasdsadadasdasdasdadasdsad: false

Did I miss something trivial here or this is not possible using regex? It seems like it should be.

我在这里想念一些微不足道的事情,或者使用正则表达式这是不可能的?看起来应该是这样。

Feel free to suggest a better title, I tried to be as descriptive as possible.

随意建议一个更好的标题,我试图尽可能描述。

2 个解决方案

#1


1  

Change your regular expression to: "^(?![\\s.])([A-Za-z]{10,46}$)" and it will work.

将正则表达式更改为:“^(?![\\ s。])([A-Za-z] {10,46} $)”它将起作用。

\s refers to any whitespace and you need to escape the \ inside the string and that's why it becomes \\s.

\ s引用任何空格,你需要转义字符串内的\,这就是它成为\\ s的原因。

You can also check this link

您也可以查看此链接

#2


1  

You need to turn on compiler warnings. It would have told you that you have an unknown escape sequence in your regex. I recommend using a raw literal.

您需要打开编译器警告。它会告诉你,你的正则表达式中有一个未知的转义序列。我建议使用原始文字。


#include <iostream>
#include <regex>

int main() {
    std::string randomStrings[] = { " hello", " hellllloooo", ".hello",
            ".zoidbergvddd", "asdasdsadadasdasdasdadasdsad" };

    std::regex txt_regex(R"foo(^(?!\ |\.)([A-Za-z]{10,46}$))foo");

    for (const auto& str : randomStrings) {
        std::cout << str << ": " << std::boolalpha
                  << std::regex_match(str, txt_regex) << '\n';
    }
}

clang++-3.8 gives

hello: false

hellllloooo: false

.hello: false

.zoidbergvddd: false

asdasdsadadasdasdasdadasdsad: true

#1


1  

Change your regular expression to: "^(?![\\s.])([A-Za-z]{10,46}$)" and it will work.

将正则表达式更改为:“^(?![\\ s。])([A-Za-z] {10,46} $)”它将起作用。

\s refers to any whitespace and you need to escape the \ inside the string and that's why it becomes \\s.

\ s引用任何空格,你需要转义字符串内的\,这就是它成为\\ s的原因。

You can also check this link

您也可以查看此链接

#2


1  

You need to turn on compiler warnings. It would have told you that you have an unknown escape sequence in your regex. I recommend using a raw literal.

您需要打开编译器警告。它会告诉你,你的正则表达式中有一个未知的转义序列。我建议使用原始文字。


#include <iostream>
#include <regex>

int main() {
    std::string randomStrings[] = { " hello", " hellllloooo", ".hello",
            ".zoidbergvddd", "asdasdsadadasdasdasdadasdsad" };

    std::regex txt_regex(R"foo(^(?!\ |\.)([A-Za-z]{10,46}$))foo");

    for (const auto& str : randomStrings) {
        std::cout << str << ": " << std::boolalpha
                  << std::regex_match(str, txt_regex) << '\n';
    }
}

clang++-3.8 gives

hello: false

hellllloooo: false

.hello: false

.zoidbergvddd: false

asdasdsadadasdasdasdadasdsad: true