<?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[how to use regex to verify a certain string pattern?]]></title><description><![CDATA[<p dir="auto">Hi,<br />
My aim to check whether a user has entered the input correctly. The input is a std::string which should be a polynomial. The following are two examples of what should be valid or invalid:</p>
<pre><code>    std::string sample1 = {"4x^3 - 9x^2 + 10x^1 -5x^0 -45x^-1"}; // valid
    std::string sample2 = {"4x^3 - 9x^2 + 10x -5 -45x^-1"};// invalid
</code></pre>
<p dir="auto">Right now I am trying to use std::regex to verify this:</p>
<pre><code>bool verifyPolynomial(const std::string&amp; poly){
     // Used when your searching a string
    std::smatch matches;
    
    std::regex _regex("[0-9]+[a-z]+\\^[0-9]");// I don't think this is correct?
    
    std::regex_search(poly, matches, _regex);
    
    return matches.size();
}
</code></pre>
<p dir="auto">I am learning regex, so I find this a bit confusing if someone can help that would be great</p>
]]></description><link>https://forum.qt.io/topic/141405/how-to-use-regex-to-verify-a-certain-string-pattern</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 02:14:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/141405.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Dec 2022 17:45:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to how to use regex to verify a certain string pattern? on Sun, 11 Dec 2022 19:19:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hft_developer">@<bdi>HFT_developer</bdi></a><br />
This is not an easy one for a beginner!  Try playing with <a href="https://regex101.com" target="_blank" rel="noopener noreferrer nofollow ugc">https://regex101.com</a>, where you can test regular expressions against whatever input you give it.</p>
<p dir="auto">The final answer depends on how strict you want to be, what you do/do not want to allow, whether it must match the whole, complete string, how you want to handle whitespace, and so on.</p>
<p dir="auto">I see your attempt as close:</p>
<pre><code>[0-9]+[a-z]+\^-?[0-9]+
</code></pre>
<p dir="auto">might do you for each polynomial in the expression.</p>
<p dir="auto">Then you may have to go:</p>
<pre><code>[0-9]+[a-z]+\^-?[0-9]+(\s*[+-]\s*[0-9]+[a-z]+\^-?[0-9]+)*
</code></pre>
<p dir="auto">to do the series of polys separated by a <code>+</code> or <code>-</code> symbol.  Or something like that :)</p>
]]></description><link>https://forum.qt.io/post/739869</link><guid isPermaLink="true">https://forum.qt.io/post/739869</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 11 Dec 2022 19:19:38 GMT</pubDate></item><item><title><![CDATA[Reply to how to use regex to verify a certain string pattern? on Mon, 12 Dec 2022 20:45:20 GMT]]></title><description><![CDATA[<p dir="auto">mathematically speaking both expressions are valid. -5x^0 evalutates to -5 so the term is the same even if it is expressed differently.</p>
]]></description><link>https://forum.qt.io/post/739981</link><guid isPermaLink="true">https://forum.qt.io/post/739981</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Mon, 12 Dec 2022 20:45:20 GMT</pubDate></item><item><title><![CDATA[Reply to how to use regex to verify a certain string pattern? on Sun, 11 Dec 2022 21:11:43 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Qt also have <a href="https://doc.qt.io/qt-6/qregularexpression.html" target="_blank" rel="noopener noreferrer nofollow ugc">QRegularExpression</a> for that kind of work and there's a <a href="https://doc.qt.io/qt-6/qtwidgets-tools-regularexpression-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">helper tool</a> that you can build to test your expressions.</p>
]]></description><link>https://forum.qt.io/post/739872</link><guid isPermaLink="true">https://forum.qt.io/post/739872</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 11 Dec 2022 21:11:43 GMT</pubDate></item><item><title><![CDATA[Reply to how to use regex to verify a certain string pattern? on Sun, 11 Dec 2022 19:19:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hft_developer">@<bdi>HFT_developer</bdi></a><br />
This is not an easy one for a beginner!  Try playing with <a href="https://regex101.com" target="_blank" rel="noopener noreferrer nofollow ugc">https://regex101.com</a>, where you can test regular expressions against whatever input you give it.</p>
<p dir="auto">The final answer depends on how strict you want to be, what you do/do not want to allow, whether it must match the whole, complete string, how you want to handle whitespace, and so on.</p>
<p dir="auto">I see your attempt as close:</p>
<pre><code>[0-9]+[a-z]+\^-?[0-9]+
</code></pre>
<p dir="auto">might do you for each polynomial in the expression.</p>
<p dir="auto">Then you may have to go:</p>
<pre><code>[0-9]+[a-z]+\^-?[0-9]+(\s*[+-]\s*[0-9]+[a-z]+\^-?[0-9]+)*
</code></pre>
<p dir="auto">to do the series of polys separated by a <code>+</code> or <code>-</code> symbol.  Or something like that :)</p>
]]></description><link>https://forum.qt.io/post/739869</link><guid isPermaLink="true">https://forum.qt.io/post/739869</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 11 Dec 2022 19:19:38 GMT</pubDate></item></channel></rss>