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);
Related
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
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");
This question already has answers here:
Ruby: Split string at character, counting from the right side
(6 answers)
Closed 8 years ago.
I have the following string
jenkins-client-1.4
perl-5.16
ruby-1.9
10gen-mms-agent-1.0
Is it possible to use regular expression to extract terms with the dash and version stripped out to end up with something like the following?
jenkins-cleint
perl
ruby
10gen-mms-agent
Thx,
-peter
Example for C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string[] terms = new string[] {
"jenkins-client-1.4",
"perl-5.16",
"ruby-1.9",
"10gen-mms-agent-1.0"
};
Regex termRegex = new Regex(#"^(.+)-(\d+[.]\d+)$");
foreach (string term in terms)
if (termRegex.IsMatch(term))
Console.WriteLine(termRegex.Match(term).Groups[1].Value);
Console.ReadLine();
}
}
This will do it: (.*?)-[0-9][0-9.]+
.*? matches anything, but as little as possible. This is the capture group you want to extract.
-[0-9][0-9.] matches a hyphen, then a digit, then any number of digits and periods.
This question already has an answer here:
Get last match with Boost::Regex
(1 answer)
Closed 9 years ago.
Somehow, I've failed to find out, how to put only the first occurrence or regular expression to string. I can create a regex object:
static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
Now, I need to match ([A-Za-z0-9_]+) to std::string, say playername.
std::string chat_input("<Darker> Hello");
std::string playername = e.some_match_method(chat_input, 1); //Get contents of the second (...)
What have I missed?
What should be instead of some_match_method and what parameters should it take?
You can do something like this:
static const regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
string chat_input("<Darker> Hello");
smatch mr;
if (regex_search(begin(chat_input), end(chat_input), mr, e)
string playername = mr[2].str(); //Get contents of the second (...)
Please note that regex is part of C++11, so you don't need boost for it, unless your regular expression is complex (as C++11 and newer still has difficulties processing complex regular expressions).
I think what you're missing is that boost::regex is the regular expression, but it doesn't do the parsing against a given input. You need to actually use it as a parameter to boost::regex_search or boost::regex_match, which evaluate a string (or iterator pairs) against the regular expression.
static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
std::string chat_input("<Darker> Hello");
boost::match_results<std::string::const_iterator> results;
if (boost::regex_match(chat_input, results, e))
{
std::string playername = results[2]; //Get contents of the second (...)
}
This question already has an answer here:
std::stringstream strange behaviour
(1 answer)
Closed 9 years ago.
I have a string with lots of different characters similar to: "$: " "213.23453"
How do i extract the double value 213.23453 and store it in a variable, it's C++/C and i cant use lambdas.
You can use "poor man's regex" of the sscanf function to skip over the characters prior to the first digit, and then reading the double, like this:
char *str = "\"$: \" \"213.23453\"";
double d;
sscanf(str, "%*[^0-9]%lf", &d);
Note the asterisk after the first percentage format: it instructs sscanf to read the string without writing its content into an output buffer.
Here is a demo on ideone.
Use a regular expression.
[$]?[0-9]*(\.)?[0-9]?[0-9]?
This should match those with a $ sign and those without.
Boost.Regex is a very good regular expression library
Personally, I find Boost.Xpressive much nicer to work with. It is a header-only library and it has some nice features such as static regexes (regexes compiled at compile time).
If you're using a C++11 compliant compiler, use std::regex unless you have good reason to use something else.
Pure C++ solution could be to manually cut off the trash characters preceding the number (first digit identified by std::isdigit) and then just construct a temporary istringstream object to retrieve the double from:
std::string myStr("$:. :$$#&*$ :213.23453$:#$;");
// find the first digit:
int startPos = 0;
for (; startPos < myStr.size(); ++startPos)
if (std::isdigit(myStr[startPos])) break;
// cut off the trash:
myStr = myStr.substr(startPos, myStr.size() - startPos);
// retrieve the value:
double d;
std::istringstream(myStr) >> d;
but C-style sscanf with appropriate format specified would suffice here as well :)