<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QStringList::indexOf(QRegularExpression) don&#x27;t work :(]]></title><description><![CDATA[<p dir="auto">Hi, I've a problem searching a part of string in QStringList.<br />
I've this QStringList:</p>
<pre><code>QStringList *Choices = new QStringList;

    Choices-&gt;append("Languages|1|Italian");
    Choices-&gt;append("Languages|2|English");
    Choices-&gt;append("Languages|3|Spanish");
    Choices-&gt;append("Languages|4|Japanese");
    Choices-&gt;append("City|0|New York");
    Choices-&gt;append("City|1|Toronto");
    Choices-&gt;append("City|2|Rome");
    Choices-&gt;append("Color|2|Red");
    Choices-&gt;append("Color|1|Bronwn");
    Choices-&gt;append("Color|5|Black");
    Choices-&gt;append("Name|2|Stefano");
    Choices-&gt;append("Name|2|John");
    Choices-&gt;append("Name|2|Jack");
</code></pre>
<p dir="auto">and I must to search first occurence of an initial part of string.<br />
To obtain this result, I use this code:</p>
<pre><code>int16_t idx = int16_t(slChoices-&gt;indexOf(QRegularExpression("^Color")));
</code></pre>
<p dir="auto">but result is always <strong>-1</strong> :(<br />
I don't understand where I'm wrong.<br />
If I test my regular expression here: <a href="https://www.debuggex.com/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.debuggex.com/</a> it work fine.</p>
<p dir="auto">Can you help me?<br />
Thanks.</p>
<p dir="auto">Stefano</p>
]]></description><link>https://forum.qt.io/topic/114241/qstringlist-indexof-qregularexpression-don-t-work</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 17:40:06 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114241.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 29 Apr 2020 20:07:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Thu, 30 Apr 2020 09:17:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kromignon">@<bdi>KroMignon</bdi></a> : Many thanks, it works fine :)<br />
obviously many thanks also to all the others who answered me :)</p>
]]></description><link>https://forum.qt.io/post/591757</link><guid isPermaLink="true">https://forum.qt.io/post/591757</guid><dc:creator><![CDATA[Stefanoxjx]]></dc:creator><pubDate>Thu, 30 Apr 2020 09:17:27 GMT</pubDate></item><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Thu, 30 Apr 2020 08:51:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/591748">QStringList::indexOf(QRegularExpression) don't work :(</a>:</p>
<blockquote>
<p dir="auto">The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with ^...$?</p>
</blockquote>
<p dir="auto">Yes, this is the way <code>QStringList::indexOf(onst QRegularExpression &amp;re, int from = 0)</code> is working.</p>
]]></description><link>https://forum.qt.io/post/591751</link><guid isPermaLink="true">https://forum.qt.io/post/591751</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Thu, 30 Apr 2020 08:51:42 GMT</pubDate></item><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Thu, 30 Apr 2020 08:46:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kromignon">@<bdi>KroMignon</bdi></a><br />
In which case, to confirm my understanding of <a href="https://doc.qt.io/qt-5/qstringlist.html#indexOf-4" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qstringlist.html#indexOf-4</a> wording which I don't find very clear, the <code>^</code> is redundant  and <code>QRegularExpression("Color.*")</code> 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 <code>^...$</code>?</p>
]]></description><link>https://forum.qt.io/post/591748</link><guid isPermaLink="true">https://forum.qt.io/post/591748</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 30 Apr 2020 08:46:46 GMT</pubDate></item><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Thu, 30 Apr 2020 07:09:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stefanoxjx">@<bdi>Stefanoxjx</bdi></a> said in <a href="/post/591679">QStringList::indexOf(QRegularExpression) don't work :(</a>:</p>
<blockquote>
<p dir="auto">To obtain this result, I use this code:<br />
int16_t idx = int16_t(slChoices-&gt;indexOf(QRegularExpression("^Color")));</p>
</blockquote>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> has written, you need <strong>exact match</strong> for <code>QStringList::indexOf()</code>, so you have to change you regex to:</p>
<pre><code>int16_t idx = int16_t(slChoices-&gt;indexOf(QRegularExpression("^Color.*")));
</code></pre>
]]></description><link>https://forum.qt.io/post/591737</link><guid isPermaLink="true">https://forum.qt.io/post/591737</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Thu, 30 Apr 2020 07:09:19 GMT</pubDate></item><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Wed, 29 Apr 2020 20:59:22 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Are you looking for Color in the beginning of the string ?<br />
I was wondering if you can use<br />
<a href="https://doc.qt.io/qt-5/qstring.html#startsWith" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qstring.html#startsWith</a></p>
]]></description><link>https://forum.qt.io/post/591689</link><guid isPermaLink="true">https://forum.qt.io/post/591689</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 29 Apr 2020 20:59:22 GMT</pubDate></item><item><title><![CDATA[Reply to QStringList::indexOf(QRegularExpression) don&#x27;t work :( on Wed, 29 Apr 2020 20:38:05 GMT]]></title><description><![CDATA[<p dir="auto">See the <a href="https://doc.qt.io/qt-5/qstringlist.html#indexOf-4" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a>: Returns the index position of the first <strong>exact</strong> match.</p>
<p dir="auto">btw: There is no need to create a QStringList on the heap.</p>
]]></description><link>https://forum.qt.io/post/591685</link><guid isPermaLink="true">https://forum.qt.io/post/591685</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 29 Apr 2020 20:38:05 GMT</pubDate></item></channel></rss>