简体   繁体   中英

Using regex to check input validity?

I am trying to detect invalid input, where the variable n shouldn't contain any of the symbols: ;:"'[]*^%$#@! , defined in regex r , in the following code:

#include "iostream"
#include "string"
#include "sstream"
#include "regex"

using namespace std;

struct Person{

     // constructor
     Person(string n, int a)
         : name(n), age(a) { 
         if (a <= 0 || a > 150) throw std::out_of_range("Age out of range."); 

        // regex r(";:\"\'[]*^%$#@!");
        // regex r("\:|\;|\"|\'|\[|\]|\*|\^|\%|\$|\#|\@|\!");
        // regex r("/[\:\;\"\'\[\]\*\^\%\$\#\@\!]/");
        // regex r("/[;:\"\'[]*^%$#@!]/");

        smatch matches;
        regex_match(n, matches ,r);
        if (!matches.empty()) throw std::invalid_argument("Name contains invalid symbols.");
    }

    // data members
    string name;
    int age;
};

//-----------------------------------------------------------------------------------------
int main(){

   try{

    vector<Person> people;

    string input_termination = "end";
    while(true){
        cout <<"Type name and age; terminate with \"end\":\n>>";

        string line;
        getline(cin, line);

        stringstream ss(line);

        string n;
        int a;

        ss >> n >> a;

        if (n == input_termination) break;
        else people.emplace_back(Person(n,a));
    }

    cout <<"\nStored people: \n";
    for (auto it = people.begin();  it != people.end(); ++it) cout << *it <<'\n';

    } catch (exception& e){
        cerr << e.what() << endl;
        getchar();
    } catch (...){
        cerr <<"Exception!" << endl;
        getchar();
    }

}

The commented lines are all the unsuccessful attempts which either result in no throw 1 or in the following error message:

regular expression error

How to properly define and use regex in the above constructor such that n is detected if it contains any of the forbidden symbols?

Note: I've read the suggested sources.


1. When an invalid name, containing some of the symbols, is used to initialize an object.

the main problem is that there are certain special characters that need to be escaped with a \\ character in order for the regex engine to read it as itself (ie, * is a special token meaning to match 0 or more of the previous token). This means that not only do you need to escape the usual ' && " characters, there are other ones you need to prefix with a \\ char

you can than achieve what you want with a pattern like:

";:\\\"\\\'\[\]\*\^%\$#@!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM