C++ regex character class not matching [duplicate] - c++

This question already has answers here:
Trouble with C++ Regex POSIX character class
(3 answers)
Closed 4 years ago.
from what i researched, the expression "[:alpha:]" will be matched for any alphabetic character, but the expression only match for lowercase character and not uppercase character. I not sure what's wrong with it.
std::regex e ("[:alpha:]");
if(std::regex_match("A",e))
std::cout<<"hi";
else
std::cout<<"no";

Change this:
std::regex e ("[:alpha:]");
to:
std::regex e ("[[:alpha:]]");
As Adrian stated: Please note that the brackets in the class names are additional to those opening and closing the class definition. For example: [[:alpha:]] is a character class that matches any alphabetic character. Read more in the ref.

You have to use [[:alpha:]]
see online example
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
std::regex e ("[[:alpha:]]");
if(std::regex_match("A",e))
std::cout<<"hi";
else
std::cout<<"no";
return 0;
}

Related

C++ std::regex not support look ahead backreference

I want to match a rule, some name occur like ABA, And A must not equal B.
for example,
'allen_bob_allen' is valid.
'allen_allen_allen' is not valid.
at https://regexr.com/, I wrote the regex pattern
([a-z]+)_(?!\1)([a-z]+)_\1 and it work perfectly,
But When I write this in C++, use std::regex,
using namespace std;
#include <regex>
int main() {
std::regex regex_test2("([a-z]+)_(?<!\\1)([a-z]+)_\\1");
return 0;
}
I got the following error:
libc++abi.dylib: terminating with uncaught exception of type std::__1::regex_error: The expression contained an invalid back reference.
here is my compiler info:
Is there anyway to fix this?

c++ regex won't match spaces [duplicate]

This question already has an answer here:
\s not working in C++ regex
(1 answer)
Closed 3 years ago.
Please help............................................
My regex won't work on spaces:
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<fstream>
#include<list>
#include<numeric>
#include<map>
#include<iterator>
#include<regex>
using namespace std;
int main()
{
regex date3{R"([A-Z][a-z]{2,}\s\d{1,})"};
string s;
cin>>s;
smatch matches;
if(regex_match(s,matches,date3)){
cout<<matches[0];
}
return 0;
}
I also tried
regex date3{R"([A-Z][a-z]{2,} \d{1,})"};
For example, I input May 3 and it outputs nothing.
cin>>s splits the input on white space and discards those white spaces.
Instead you may want to use for instance getline(cin, s).

Matching single digit with std::regex_match

According to this reference, I should be able to match a single digit with
std::regex e1 ("\\d");
However, when I run the following test code I get a regex exception.
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::regex r("\\d");
std::string s("9");
if (std::regex_match(s, r)) { std::cout << "matched!" << std::endl; }
}
GCC's std::regex support is not yet ready for prime time. See: Is gcc 4.8 or earlier buggy about regular expressions?
If std::regex support is still buggy as #qwrrty suggests, the character class '[0-9]' is a substitute for '\d'.

STL and Regular Expression

I'm trying to write a string parser that uses the standard library methods in C++. I want to parse out of an incoming string substrings that end with a newline or a ';'. I keep getting exceptions from the regex object that I create. My pattern is:
string pattern = "(.+[\\n\\r;])";
regex cmd_sep(pattern);
I've tried it with and without the regex_constants::extended or basic flags.
You'd better post your error message, if you are using boost library. It is possible you've missed boost::regex tag.
Try this
#include <boost/regex.hpp>
#include <string>
using namespace std;
int main ()
{
string pattern = "(.+[\\n\\r;])";
static const boost::regex cmd_sep(pattern);
return 0;
}

expected expression in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No output for cout
i writed this code in c++ for my uni , but i have an error in return 0 , the code don't work . i am using xcode to develop
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
std::cout<<portF<<
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
std::cout<<portF<<
return 0;
}
std::cout<<portF<<
should be
std::cout<<portF;
Note you've made the same error twice. A semicolon is what ends a statement. When you put an insertion operator instead of it, compiler expects another expression (and that's what it is telling you).
use:
std::cout<<portF;
instead of,
std::cout<<portF<<
1. You haven't added a semicolon after the statement.
2. you are using one extra << operator
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
// You have to end this statement with semi colon
std::cout<<portF;
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
// Similarily here
std::cout<<portF;
return 0;
}