Need QRegEx logic to split number bullet - qt

I am trying to build a QRegEx logic to split number bullet. I tried but couldn't succeed.
sample code:
QString query("1. Ravi Gupta.Pari.Paagal");
QStringList list = query.split(QRegularExpression("\\(.*?\\)"));
qDebug()<<"Output: "<<list;
I am using QRegEx first time. Looking for some help here.
Sample text is --> "1. Ravi Gupta.Pari.Paagal"
Required output should be --> "Ravi Gupta.Pari.Paagal" (without number bullet)

I'm not sure why you're using QString::split. If the intention is simply to obtain that part of the line after the numbered bullet you could use something like...
QString text("1. Ravi Gupta.Pari.Paagal");
QRegularExpression re("^\\d+\\.\\s+(.*)$");
if (auto match = re.match(text); match.hasMatch()) {
std::cout << "found match [" << match.captured(1) << "]\n";
}
Should give...
found match [Ravi Gupta.Pari.Paagal]

Related

How to remove double qoutes in Objective-C

Let me introduce myself.
My name is Vladimir, C++ programmer, I am from Serbia. two weeks ago I have started to learn objective-C and it was fine until tonight.
Problem:
I cant remove double quotes from my NSLog output.
NSLog(#"The best singers:%#", list.best);
Strings are joined with componentsJoinedByString:#" and "
I would like to get something like this:
The best singers: Mickey and John.
But I get this:
The best singers: ("Mickey", and "John").
I cant remove comma (,) and parentheses either.
I have tried with "replaceOccurencesOfString" but with no success. It can remove any character except qoute and comma.
Also I have used -(NSString *)description method to return string.
You are getting the raw output from your list (which I assume is an array). You will have to do your own formatting to get this to display in the format that you want. You can achieve this by building your string by iterating through your array. Note that this probably isn't the most efficient nor the most robust way to achieve this.
NSMutableString *finalString = [NSMutableString string];
BOOL first = YES;
for (NSString *nameString in list) {
if (first) {
[finalString appendString:nameString];
first = NO;
} else {
[finalString appendString:[NSString stringWithFormat:#" and %#", nameString]];
}
}

Unit test failing on two equal Qstrings (one is read from a file)?

I'm trying to compare two string which are supposed to be the same. The test however fails.
I'm testing if the file get set correctly by the router.setForwarding(true) method.
Here is the code of the test.
void router_test::testSetForwarding_true()
{
QFile myfile("/proc/sys/net/ipv4/ip_forward");
myfile.open(QIODevice::ReadOnly | QIODevice::Text);
router->setForwarding(true);
QString forward = QString(myfile.readAll());
QCOMPARE(QString("1"),forward);
}
As a result I get:
FAIL! : router_test::testSetForwarding_true() Compared values are not the same
Actual (QString("1")): 1
Expected (forward): 1
Why aren't they equal?
As you can glean from the output, you have interchanged the actual and expected values. You're also comparing the newline-terminated output against one without a newline.
This should work:
QCOMPARE(forward, QString("1\n"));
or
QCOMPARE(forward[0], QChar('1'));

loading csv in qtablewidget (Why my code is not working?)

I am self-learner and new to qt(I just want to learn to write program).
Trying to load the csv file to qtablewidget but it overriding all columns and row with same data. I tried to follow How to import a CSV file to a QTableWidget but i did not get it correctly.
Code:
QFile file("testData.txt");
QTextStream in(&file);
QStringList setDataInrow;
QStringList rowNumbers;
QString allLine;
if(file.open(QIODevice::ReadOnly)){
allLine=in.readAll();
rowNumbers=allLine.split("\n");
file.close();
}
QTableWidget *myTable=new QTableWidget();
myTable->setRowCount(rowNumbers.size());
for(int row=0;row<rowNumbers.count();row++)
{
setDataInrow=allLine.split(";");
for(int column=0;column<setDataInrow.count();column++){
myTable->setColumnCount(setDataInrow.size());
//myTable->item(row,column)->setText(setDataInrow[column]);
QTableWidgetItem *item=new QTableWidgetItem(setDataInrow[column]);
myTable->setItem(row,column,item);
}
}
qDebug()<<"Numbers of row needed:"<<"\n"<<rowNumbers<<"\n";
qDebug()<<"Set following data to each column as per row:"<<"\n"<<setDataInrow<<setDataInrow.size();
window->setCentralWidget(myTable);
window->show();
return app.exec();
}
i am trying to load :
John Kl;34;1335532;CA;0444344
Kuma jo;54;44432;NY;0322355
Lebal ho;24;44022;NY;0110004
It should be loaded within 3 rows and 5 columns but it is setting 13 columns . Perhaps i am not able think this correctly. I need your help with some code example so that i can study about it more.
Since my English is not good(Sucks like my codes :)) I have taken an screenshot of the current program which is not working as expected:
http://imageshack.us/a/img801/1601/le59.png
setDataInrow=allLine.split(";");
You do this once for each line. But you don't split the line, as you intended, but each time the whole file content.
Edit:
Don't split allLine. You have already a QStringList, which contains your lines: rowNumbers.
This you must split.
for(int row=0;row<rowNumbers.count();row++){
QStringList rowCells = rowNumbers.at(row).split(";");
.....
}
I hope this gives you the idea. No guarantee for details.
Edit 2:
When you do
setDataInrow=allLine.split(";");
your stringlist contains:
John Kl 34 1335532 CA 0444344 Kuma jo 54 44432 NY 0322355 Lebal ho 24 44022 NY 0110004.
This is the whole text in your file. The line breaks do not matter. They are just another character. You add all this in one row. And you do it three times.
What you want is first split the text into lines. You already do this here: rowNumbers=allLine.split("\n");
With your example data the rowNumbers stringlist contains three entries. Each entry one line from your file. These lines you have to split.

what is the fastest way to replace multiple words in a very long string simultaneously

Suppose I have a string which contains more than 10,000 words, for example, it is the contents of the famous fiction "The old man and Sea"
and a dictionary which have 1,000 words pairs,for example,
before,after
--------------
fish , net
black, white
good, bad
....
....
round,rect
so what I want to do is ,according to the dictionary ,replace all 'fish' in the string with 'net', 'black' with 'white' ....
the simplest and intuitive algorithm is :
foreach(line, dict)
str.replace(line.before,line.after)
but it was so inefficiency.
one improvement I can think of is to
separate the string to multiple small string, then
use multithread to handle each smallstring respectively ,then combine the result.
is there any other ideas?
btw, I am using QT
I think it's a better idea to have a vector of 10k words, not a string of characters.
Just like this:
QVector<QString> myLongString;
Your dictionary can be implemented as a hash table:
QHash<QString, QString> dict;
This will provide constant access time to your dictionary words:
QString replaceWith = dict.value("fish") // replaceWith == "net"
Then you can iterate through your vector and replace words:
for (int i=0; i < myLongString.size(); ++i)
{
QString word = myLongString[i];
if dict.contains(word)
{
myLongString[i] = dict.value(word);
}
}

filled with zeros

How can I fill with zeros at left? My code is:
QString reelId = QString("Month %1").arg(QDate::currentDate().month());
qDebug() << reelId;
and out is: Month 2 and I want Month 02;
The term you're really looking for is "pad", as in padding a string with something. Look through the QString documentation for this, specifically the various QString::arg() functions, which describes how to achieve this in a variety of ways :)
Have a look at QString::rightJustified()
QString reelId = QString( "Month %1" ).arg(
QString::number( QDate::currentDate().month() ).rightJustified( 2, '0' )
);
Another useful trick is to use QDate's toString() method thus:
QString reelId = QDate::currentDate().toString( "'Month' MM" );
The single quotes around "Month" prevent the word being interpreted as a placeholder.

Resources