Skip to content

Instantly share code, notes, and snippets.

@the-nose-knows
Last active April 10, 2017 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-nose-knows/8e59d7ed6b31cb97957d6ef810b1c3f8 to your computer and use it in GitHub Desktop.
Save the-nose-knows/8e59d7ed6b31cb97957d6ef810b1c3f8 to your computer and use it in GitHub Desktop.
#include <QCoreApplication>
#include <QRegularExpressionMatch>
#include <QStringList>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QStringList results;
QRegularExpression this_regex("(\\wog)|(\\wee)");
QString test_string = "dog pog log moo dew tree glee dee";
if(!this_regex.isValid())
{
std::cerr << "Invalid regex pattern: " << this_regex.pattern().toStdString() << std::endl;
return -2;
}
for (int i = 0; i < this_regex.captureCount()+1; ++i)
{
// This skips storing capture-group 0 if any capture-groups were actually specified.
// If they weren't, capture-group 0 will be the only thing returned.
if((i!=0) || (this_regex.captureCount() < 1))
{
QRegularExpressionMatchIterator iterator = this_regex.globalMatch(test_string);
while (iterator.hasNext())
{
QRegularExpressionMatch match = iterator.next();
QString matched = match.captured(i);
// Remove this if-check if you want to keep zero-length results
if(matched.length() > 0){results << matched;}
}
}
}
if(results.length()==0){return -1;}
for(int i = 0; i < results.length(); i++)
{
std::cout << results.at(i).toStdString() << std::endl;
auto temp = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment