Hello.
How can you implement the following?
There is a variable that contains, for example, the text " privet-hash-code; ". This variable should be checked by such a regular " [^\a-zA-Z0-9] " (the text of the variable no longer fits this rule). So, how to make it so that if the text of a variable does not fit, then the characters, which should not be, are simply removed?
Those. was " privet-hash-code; ", and should be " privethashcode ".

Thank.

    2 answers 2

    You can like this:

     $var = 'privet-hash-code;'; $result = ''; preg_match('/([a-zA-Z0-9]+)/', $var, $matches); if(!empty($matches[1])){ $result = implode('', $matches[1]); } 

      I hope that I understood you correctly.


      You can separately store 2 versions of the regular schedule - to check the validity of the entire text and to check the validity of a single character.

      If check(string, первое_регулярное_выражение) says that string does not fall under the первое_регулярное_выражение , then do strip(string, второе_регулярное_выражение) , that is, remove those characters that do not fall under it. The second part, by the way, can be optimized, if you do not want to deal with regular expressions for one character.


      If you are talking about a more general task - as an arbitrarily taken expression by a finite number of changes, to bring about the appearance of some regexp'а (or report that such a display is impossible) , then this is already a topic for a separate article in the ACM Digital Library .

      Here you can try to build some kind of finite state machine or suffix tree and take the Levenshtein distance as a metric, but before doing this, you should look in Google for relevant articles.

      The point is that the standard tools for option number 2, presumably, no.