Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course it's possible with regex! You're not asking to match nested parentheses! :P</p> <p>But yes, this is the kind of thing regular expressions were built for. An example:</p> <pre><code>/\S*[^\w\s]+\S*/ </code></pre> <p>This will match all of the following:</p> <pre><code>@ss as$ a$s @$s a$$ @s$ @$$ </code></pre> <p>It will <em>not</em> match this:</p> <pre><code>ass </code></pre> <p>Which I believe is what you want. How it works:</p> <p><code>\S*</code> matches 0 or more non-space characters. <code>[^\w\s]+</code> matches only the symbols (it will match anything that isn't a word or a space), and matches 1 or more of them (so a symbol character is required.) Then the <code>\S*</code> again matches 0 or more non-space characters (symbols and letters).</p> <p>If I may be allowed to suggest a better strategy, in Perl you can store a regex in a variable. I don't know if you can do this in PHP, but if you can, you can construct a list of variables like such:</p> <pre><code>$a = /[aA@]/ # regex that matches all a-like symbols $b = /[bB]/ $c = /[cC(]/ # etc... </code></pre> <p>Or:</p> <pre><code>$regex = array( 'a' =&gt; /[aA@]/, 'b' =&gt; /[bB]/, 'c' =&gt; /[cC(]/, ... ); </code></pre> <p>So that way, you can match "friend" in all its permutations with:</p> <pre><code>/$f$r$i$e$n$d/ </code></pre> <p>Or:</p> <pre><code>/$regex['f']$regex['r']$regex['i']$regex['e']$regex['n']$regex['d']/ </code></pre> <p>Granted, the second one looks unnecessarily verbose, but that's PHP for you. I think the second one is probably the best solution, since it stores them all in a hash, rather than all as separate variables, but I admit that the regex it produces is a bit ugly.</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