C++ , Regular expression - c++

I know how to find regular expression in specific string. How to find first element that match with regular expression?
Here is my code:
QString mangledText;
QRegExp rx("string");
while ((pos = rx.indexIn(mangledText)) != -1){
mangledText.replace(pos, rx.matchedLength(), "replaced string");
}
I want to replace first match result (or second or third) instead of all of that.
Any suggestion?

I want to replace first match result instead of all of that.
Use an if instead of a while.
if ((pos = rx.indexIn(mangledText)) != -1){
mangledText.replace(pos, rx.matchedLength(), "replaced string");
}

Related

Extract the string between two words using RegEx in QT [duplicate]

can anybody help me with this?
I have a string which contains N substrings, delimited by tags and I have to get ALL of the substrings. The string is like
STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND
I would like to get all the strings between START/END tags, I tried with a couple of regular expressions with no luck:
(START)(.*)(END) gives me ALL the contend between the first and last tag
(START)(\w+)(END) gives me no result
The code is much simple:
QString l_str "STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND";
QRegExp rx("(START)(\w+)(END)");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(l_str, pos)) != -1)
{
list << rx.cap(1);
pos += rx.matchedLength();
}
qWarning() << list;
I'd like a resulting list like:
STARTfoo barEND
STARThi there!END
STARTstackoverflowrulezEND
Any help?
Thanks!
Use rx.setMinimal(true) with .* to make it lazy:
QRegExp rx("START.*END");
rx.setMinimal(true);
See the QRegExp::setMinimal docs:
Enables or disables minimal matching. If minimal is false, matching is greedy (maximal) which is the default.

Find strings between two tags with regex in Qt

can anybody help me with this?
I have a string which contains N substrings, delimited by tags and I have to get ALL of the substrings. The string is like
STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND
I would like to get all the strings between START/END tags, I tried with a couple of regular expressions with no luck:
(START)(.*)(END) gives me ALL the contend between the first and last tag
(START)(\w+)(END) gives me no result
The code is much simple:
QString l_str "STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND";
QRegExp rx("(START)(\w+)(END)");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(l_str, pos)) != -1)
{
list << rx.cap(1);
pos += rx.matchedLength();
}
qWarning() << list;
I'd like a resulting list like:
STARTfoo barEND
STARThi there!END
STARTstackoverflowrulezEND
Any help?
Thanks!
Use rx.setMinimal(true) with .* to make it lazy:
QRegExp rx("START.*END");
rx.setMinimal(true);
See the QRegExp::setMinimal docs:
Enables or disables minimal matching. If minimal is false, matching is greedy (maximal) which is the default.

Conditional replace string using boost::regex_replace

I want to simplify the signs in a mathematical expression using regex_replace, here is a sample code:
string entry="6+-3++5";
boost::regex signs("[\-\+]+");
cout<<boost::regex_replace(entry,signs,"?")<<endl;
The output is then 6?3?5. My question is: How can I get the proper result of 6-3+5 with some neat regular expression tools? Thanks a lot.
Tried something else with sregex_iterator and smatch, but still has some problem:
string s="63--17--42+5555";
collect_sign(s);
Output is
63+17--42+5555+42+5555+5555
i.e.
63+(17--42+5555)+(42+5555)+5555
It seems to me that the problem is related to the match.suffix(), Could anybody help please? The collect_sign function basically just iterate through every sign strings, convert it to "-"/"+" if the number of "-" is odd/even, and then stitch together the suffix expression of the signs.
void collect_sign(string& entry)
{
boost::regex signs("[\-\+]+");
string output="";
auto signs_begin = boost::sregex_iterator(entry.begin(), entry.end(), signs);
auto signs_end = boost::sregex_iterator();
for (boost::sregex_iterator it = signs_begin; it != signs_end; ++it)
{
boost::smatch match = *it;
if (it ==signs_begin)
output+=match.prefix().str();
string match_signs = match.str();
int n_minus=count(match_signs.begin(),match_signs.end(),'-');
if (n_minus%2==0)
output+="+";
else
output+="-";
output+=match.suffix();
}
cout<<"simplify to: "<<output<<endl;
}
Use:
[+\-*\/]*([+\-*\/])
Replace:
$1
You can test here
If you just want a mathematical simplification, you can use:
s = boost::regex_replace(s, boost::regex("(?:++|--"), "+", boost::format_all);
s = boost::regex_replace(s, boost::regex("(?:+-|-+"), "-", boost::format_all);

Qt and QtRegExp for parsing html tags

I want to parse out html tag name. My code is this:
QRegExp exp("<\\s*(\\w+)\\s*");
exp.indexIn("<html> hi there </html>");
qDebug() << exp.cap(1);
It's logging "h" instead of "html". Why? As far as I understand it, the \w+ should find a string with one or more word characters, in this case "html". But since it's not, what would be the right way to achive this?
That's because you are using indexIn without iterating the matches and you aren't using the capturing groups to access to your captured data.
Use this code instead:
QRegExp rx("<\\s*(\\w+)\\s*");
QString str = "<html> hi there </html>";
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
list << rx.cap(1);
pos += rx.matchedLength();
}
// list: ["html"]

Regex to filter strings

I need to filter strings based on two requirements
1) they must start with "city_date"
2) they should not have "metro" anywhere in the string.
This need to be done in just one check.
To start I know it should be like this but dont know hoe to eliminate strings with "metro"
string pattern = "city_date_"
Added: I need to use the regex for a SQL LIKE statement. hence i need it in a string.
Use a negative lookahead assertion (I don't know if this is supported in your regex lib)
string pattern = "^city_date(?!.*metro)"
I also added an anchor ^ at the start, that will match the start of the string.
The negative lookahead assertion (?!.*metro) will fail, if there is the string "metro" somewhere ahead.
Regular expressions are usually far more expensive than direct comparisons. If direct comparisons can easily express the requirements, use them. This problem doesn't need the overhead of a regular expression. Just write the code:
std::string str = /* whatever */
const std::string head = "city_date";
const std::string exclude = "metro";
if (str.compare(head, 0, head.size) == 0 && str.find(exclude) == std::string::npos) {
// process valid string
}
by using javascript
input="contains the string your matching"
var pattern=/^city_date/g;
if(pattern.test(input)) // to match city_data at the begining
{
var patt=/metro/g;
if(patt.test(input)) return "false";
else return input; //matched string without metro
}
else
return "false"; //unable to match city_data