Question

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

preg_match_all('/#(\w+)/', $string, $matches);
Was it helpful?

Solution

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'

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top