Pergunta

How can I implement this to skip characters like: ! : \ & ' in any position?

preg_match_all('/#(\w+)/', $string, $matches);
Foi útil?

Solução

If you were to use a pattern like the following, depending on the implementation, you should be able to find all of those characters and substitute them out:

$teststring = "#!TEST'\&"
$pattern = (!|&|\\|\')
$replacement = ''
echo preg_replace($pattern, $replacement, $teststring)

Should output:

'#TEST'

Outras dicas

First, strip the string of all undesired characters. A regex substitution like [!:\\&'] is one way to do this, although there are likely better ways to do it using your programming language's string API.

Then parse the resulting string using your regex.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top