Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you just want to support <code>?</code>, that's pretty easy: when you encounter a <code>?</code> in the pattern, just skip ahead over one byte of input (or check for <code>isalpha</code>, if you really meant you only want to match letters).</p> <p>Edit: Assuming the more complex problem (finding a match starting at any position in the input string), you could use code something like this:</p> <pre><code>#include &lt;string&gt; size_t match(std::string const &amp;pat, std::string const &amp;target) { if (pat.size() &gt; target.size()) return std::string::npos; size_t max = target.size()-pat.size()+1; for (size_t start =0; start &lt; max; ++start) { size_t pos; for (pos=0; pos &lt; pat.size(); ++pos) if (pat[pos] != '?' &amp;&amp; pat[pos] != target[start+pos]) break; if (pos == pat.size()) return start; } return std::string::npos; } #ifdef TEST #include &lt;iostream&gt; int main() { std::cout &lt;&lt; match("??cd?", "aaaacdxyz") &lt;&lt; "\n"; std::cout &lt;&lt; match("?bc", "abc") &lt;&lt; "\n"; std::cout &lt;&lt; match("ab?", "abc") &lt;&lt; "\n"; std::cout &lt;&lt; match("ab?", "xabc") &lt;&lt; "\n"; std::cout &lt;&lt; match("?cd?", "cdx") &lt;&lt; "\n"; std::cout &lt;&lt; match("??cd?", "aaaacd") &lt;&lt; "\n"; std::cout &lt;&lt; match("??????", "abc") &lt;&lt; "\n"; return 0; } #endif </code></pre> <p>If you only want to signal a yes/no based on whether the whole pattern matches the whole input, you do pretty much the same thing, but with the initial test for <code>!=</code> instead of <code>&gt;</code>, and then basically remove the outer loop.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload