Check pipe symbol with regex

Pipe symbol | used in a regular expression as an “operator” as far as I know. Either this, or this, or that, or that, etc.

However, I want to check the regex string for this character. I tried to slip away from him, but that doesn't work.

How to do this, especially if I already check for the presence of several characters, for example #, &, @, etc.

+4
source share
2 answers
 $ cat 1933190.php <?php echo preg_match("/\|/", "This is not a pipe.") . "\n"; echo preg_match("/\|/", "The | the | this is a |.") . "\n"; ?> 

Output:

 $ php 1933190.php 0 1 
+4
source

Escaping this symbol is the right way. But make sure you also avoid the escape characters in your line declaration:

 "\\|" '\\|' 

Another way would be to use the character class ( [|] ), since you only have characters there ] , ^ (only at the beginning), - , the escape character \ and the delimiter itself, which has special meaning and therefore should be escaped if expressed as simple characters.

+7
source

All Articles