QStringList::indexOf(QRegularExpression) don't work :(
-
Hi, I've a problem searching a part of string in QStringList.
I've this QStringList:QStringList *Choices = new QStringList; Choices->append("Languages|1|Italian"); Choices->append("Languages|2|English"); Choices->append("Languages|3|Spanish"); Choices->append("Languages|4|Japanese"); Choices->append("City|0|New York"); Choices->append("City|1|Toronto"); Choices->append("City|2|Rome"); Choices->append("Color|2|Red"); Choices->append("Color|1|Bronwn"); Choices->append("Color|5|Black"); Choices->append("Name|2|Stefano"); Choices->append("Name|2|John"); Choices->append("Name|2|Jack");and I must to search first occurence of an initial part of string.
To obtain this result, I use this code:int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));but result is always -1 :(
I don't understand where I'm wrong.
If I test my regular expression here: https://www.debuggex.com/ it work fine.Can you help me?
Thanks.Stefano
-
See the documentation: Returns the index position of the first exact match.
btw: There is no need to create a QStringList on the heap.
-
Hi
Are you looking for Color in the beginning of the string ?
I was wondering if you can use
https://doc.qt.io/qt-5/qstring.html#startsWith -
Hi, I've a problem searching a part of string in QStringList.
I've this QStringList:QStringList *Choices = new QStringList; Choices->append("Languages|1|Italian"); Choices->append("Languages|2|English"); Choices->append("Languages|3|Spanish"); Choices->append("Languages|4|Japanese"); Choices->append("City|0|New York"); Choices->append("City|1|Toronto"); Choices->append("City|2|Rome"); Choices->append("Color|2|Red"); Choices->append("Color|1|Bronwn"); Choices->append("Color|5|Black"); Choices->append("Name|2|Stefano"); Choices->append("Name|2|John"); Choices->append("Name|2|Jack");and I must to search first occurence of an initial part of string.
To obtain this result, I use this code:int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));but result is always -1 :(
I don't understand where I'm wrong.
If I test my regular expression here: https://www.debuggex.com/ it work fine.Can you help me?
Thanks.Stefano
@Stefanoxjx said in QStringList::indexOf(QRegularExpression) don't work :(:
To obtain this result, I use this code:
int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));As @Christian-Ehrlicher has written, you need exact match for
QStringList::indexOf(), so you have to change you regex to:int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color.*"))); -
@Stefanoxjx said in QStringList::indexOf(QRegularExpression) don't work :(:
To obtain this result, I use this code:
int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));As @Christian-Ehrlicher has written, you need exact match for
QStringList::indexOf(), so you have to change you regex to:int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color.*")));@KroMignon
In which case, to confirm my understanding of https://doc.qt.io/qt-5/qstringlist.html#indexOf-4 wording which I don't find very clear, the^is redundant andQRegularExpression("Color.*")would match just as well? The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with^...$? -
@KroMignon
In which case, to confirm my understanding of https://doc.qt.io/qt-5/qstringlist.html#indexOf-4 wording which I don't find very clear, the^is redundant andQRegularExpression("Color.*")would match just as well? The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with^...$?@JonB said in QStringList::indexOf(QRegularExpression) don't work :(:
The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with ^...$?
Yes, this is the way
QStringList::indexOf(onst QRegularExpression &re, int from = 0)is working. -
@KroMignon : Many thanks, it works fine :)
obviously many thanks also to all the others who answered me :)