This question already has an answer here:
Regex not working as expected with C++ regex_match
(1 answer)
Closed 4 days ago.
I am very confused why this regex match in C++ not working.
#include <iostream>
#include <regex>
#include <string>
void test_code(){
const std::string test_string("this is a test of test");
const std::regex match_regex("test");
std::cout<<test_string<<std::endl;
std::smatch match;
if (std::regex_match(test_string, match, match_regex)){
std::cout<<match.size()<<std::endl;
}
}
int main() {
test_code();
}
I read the CPP reference documentation and tried to write a simple regex check. I am not sure why this is not working (i.e. it s not returning true for std::regex_match(...) call.
As stated in documentation for std::regex_match() (emphasis is mine):
Determines if the regular expression e matches the entire target character sequence, which may be specified as std::string, a C-string, or an iterator pair.
and your regex pattern does not obviously match the whole string. So you either need to change your regex to something like ".*test.*" or use std::regex_search() If you want to check substring for matching:
Determines if there is a match between the regular expression e and some subsequence in the target character sequence.
This question already has answers here:
Regex statement in C++ isn't working as expected [duplicate]
(3 answers)
Closed 3 years ago.
I need help with creating regular expressions for NMEA sentence. The reason for this because I want to validate the data whether it is a correct form of NMEA sentence. Using C++. Below is some example of NMEA sentence in the form of GLL. If it's possible I would also like to get a sample of c++ that will validate the code.
$GPGLL,5425.32,N,106.92,W,82808*64
$GPGLL,5425.33,N,106.91,W,82826*6a
$GPGLL,5425.32,N,106.9,W,82901*5e
$GPGLL,5425.32,N,106.89,W,82917*61
I have also included the expression I have tried that I found it online. But when I run it, it says unknown escape sequence.
#include <iostream>
#include <regex>
#include<string.h>
using namespace std;
int main()
{
// Target sequence
string s = "$GPGLL, 54 30.49, N, 1 06.74, W, 16 39 58 *5E";
// An object of regex for pattern to be searched
regex r("[A-Z] \w+,\d,\d,(?:\d{1}|),[A-B],[^,]+,0\*([A-Za-z0-9]{2})");
// flag type for determining the matching behavior
// here it is for matches on 'string' objects
smatch m;
// regex_search() for searching the regex pattern
// 'r' in the string 's'. 'm' is flag for determining
// matching behavior.
regex_search(s, m, r);
// for each loop
for (auto x : m)
cout << "The nmea sentence is correct ";
return 0;
}
The C++ compiler interprets \d and friends as a character escape code.
Either double the backslashes:
regex r("[A-Z] \\w+,\\d,\\d,(?:\\d{1}|),[A-B],[^,]+,0\\*([A-Za-z0-9]{2})");
or use a raw literal:
regex r(R"re([A-Z] \w+,\d,\d,(?:\d{1}|),[A-B],[^,]+,0\*([A-Za-z0-9]{2}))re");
May we have similar question here stackoverflow:
But my question is:
First I tried to match all x in the string so I write the following code, and it's working well:
string str = line;
regex rx("x");
vector<int> index_matches; // results saved here
for (auto it = std::sregex_iterator(str.begin(), str.end(), rx);
it != std::sregex_iterator();
++it)
{
index_matches.push_back(it->position());
}
Now if I tried to match all { I tried to replace
regex rx("x"); with regex rx("{"); andregex rx("\{");.
So I got an exception and I think it should throw an exception because we use {
sometimes to express the regular expression, and it expect to have } in the regex at the end that's why it throw an exception.
So first is my explanation correct?
Second question I need to match all { using the same code above, is that possible to change the regex rx("{"); to something else?
You need to escape characters with special meaning in regular expressions, i.e. use \{ regular expression. But, \ has special meaning in C++ string literals. So, next you need to escape characters with special meaning in C++ string literals, i.e. write:
regex rx("\\{");
This question already has answers here:
Excluding some character from a range - javascript regular expression
(2 answers)
Closed 3 years ago.
how can I write a regular expression to care about both condition:
1-(?:[A-Za-z][A-Za-z0-9_]*) (a simple variable name)
2-some string like :
%%
%%%
:=
()
{}
[]
<>
and ....
I want an expression to detect other chars except above groups
Edited:
I want a regular expression to just find invalid char that aren't in above groups that are alphabetic char like[a-z] and := and...
Edit2:
this question is about scanner of a mini compiler for academic project and I want to find invalid char that aren't in language char and language char are all above example like all of [A-Za-z0-9] and all %% [] {} <> <= >= () :=
in other words I want a regular expression to detect all chars that aren't in language's chars.
To summarize: How can I write an expression to find all chars that aren't [a-zA-Z0-9] or %% or %%% or := or () or {} ....
I found the answer,it's something like:
[^a-zA-Z0-9:=\]\[%{}]
This question already has answers here:
Ignore case using boost::regex_search
(2 answers)
Closed 9 years ago.
Strangely enough, google refuses to answer such simple question:
How do I make boost::regexp case-insensitive?
This is what I have:
static const boost::regex bad_words("(?:^|.* )(f(?:uc|a)k(?:i[ng]{1,2})?|bitch(?:es|iz)?)(?:$| .*)"); //reduced to the english ones
Of course, I want to filter uppercase bad words as well. This is how I match them:
//std::string ms; - chat messsage
//boost::match_results<std::string::const_iterator> results; - prewious regexp results
else if(boost::regex_match(ms, results2, bad_words)) { //
std::stringstream msg;
msg<<"Avoid bad words! Word '"<<results2[1]<<"' is banned!";
this->whisper(results[1], msg.str()); //name, message
}
So, is there another function for insensitive regexp? Or another regexp object? Or the modifier i is available?
You can use the boost::regex::icase option:
static const boost::regex bad_words("...your regex...", boost::regex::icase);