1

I've got c++ regex object

smatch matches;
regex pattern("key(\\d{3}\\w{1})"); 

By regex_search() function I'm searching with success for my pattern. As a result I execute working command: cout << matches[1]; // sub_match as output.

In my Qt application I would like to show result is QTextEdit or any other widget.

I tried:

QTextEdit *textEdit = new QTextEdit(); 
textEdit->setText(QString("%1:").arg(matches[1]));

but as a result:

error C2664: 'QString QString::arg(qlonglong,int,int,QChar) const' : cannot convert parameter 1 from 'const std::sub_match<_BidIt>' to 'qlonglong'
1>          with
1>          [
1>              _BidIt=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Could someone please give any ideas how to handle this ? I know that there is QRegExp class, but does it have similar regex_search() function ? I'd rather prefer solution with my current code.

Thanks in advance!

phyatt
  • 16,890
  • 3
  • 51
  • 70
gen_next
  • 87
  • 1
  • 11
  • You have to get the matched substring and convert it to an integer. See e.g. [`std::match_result::str`](http://en.cppreference.com/w/cpp/regex/match_results/str) and [`std::stoll`](http://en.cppreference.com/w/cpp/string/basic_string/stol). – Some programmer dude Jan 26 '14 at 22:06

2 Answers2

1

To get around the error you are seeing, you need to properly convert a std::string to a QString properly.

http://www.cplusplus.com/reference/regex/smatch/

https://stackoverflow.com/a/1814214/999943

If you are interested in using QRegEx, I've posted a number of answers about them.

https://stackoverflow.com/search?q=user%3A999943+qregex

The documentation about QRegEx isn't bad either, but some of the details of how to use it well are hidden in some of the function documentation.

http://qt-project.org/doc/qt-5/QRegExp.html

Sample Code

std::regex pattern("key(\\d{3}\\w{1})");
std::smatch matches;
std::string str = "key123a key123b";

if (std::regex_search(str, matches, pattern))
{
    qDebug() << "Match";

    for (int i = 0; i< matches.size(); i++)
    {
//            std::cout << "  submatch " << matches[i] << std::endl;
        QString outputString = QString::fromStdString(matches[i]);
        qDebug() << outputString;
    }
}
else
    qDebug() << "No match";

And a solution with just Qt stuff, based off of

http://qt-project.org/doc/qt-5/qregexp.html#indexIn

QRegExp rx("key(\\d{3}\\w{1})");
QString str = "key123a key123b";
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
    ++count;
    pos += rx.matchedLength();
    qDebug() << rx.capturedTexts();
}

Hope that helps.

Community
  • 1
  • 1
phyatt
  • 16,890
  • 3
  • 51
  • 70
0

@phyatt Thanks!

Such code works fine:

QTextEdit *textEdit = new QTextEdit;
QString outputString1 = QString::fromStdString(matches[1]);
textEdit->setText(QString("%1:").arg(outputString1));
textEdit->show();

Each time when regex conditions are met, in new window text appears: e.g. key123c

Although, how should I proceed to see this result not each time in new window but in QTextEdit windows (set through Qt Designer)? Function where above code is set is outside constructor so it's only basic: void function_from_where_results_somes();

I created other question : Access to QTextEdit widget from function

phyatt, Could you also help with this question ?

Community
  • 1
  • 1
gen_next
  • 87
  • 1
  • 11