#include

C ++: why does the regex pattern "[+ - / *]" match the string "."?

What is wrong with the regex that I used?

#include<regex>
#include<iostream>
#include<string>
using namespace std;
int main(int argc,char *argv[])
{
    smatch results;
    string temp("[+-/*]");
    string test(".");

    regex r(temp);

    if(regex_search(test,results,r))
        cout<<results.str()<<endl;
    return 0;
}

"" will print, and if I use '\' to create an escape sequence, for example:

string temp("[\\+-/\\*]");

The output remains.

+4
source share
1 answer

The problem is that it is -interpreted differently within a character class [], spanning characters in a range. This is an interpretation -that allows you to define [A-Z]to cover all uppercase letters or [0-9]to cover all decimal digits.

-, \:

string temp("[-+/*]");

[+-/] '+' (43) '/' (47) , .. '+', ',', '-', '.' '/'.

+8

Source: https://habr.com/ru/post/1613022/


All Articles