Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for the pipe symbol with Regex

Tags:

regex

php

The pipe symbol | is used in Regular Expression as a sort of 'either' statement, far as I know. Either this, either that, or that, or that, etc...

However, I wish to check a string with a regular expression, for the presence of this symbol. I've tried escaping it, but that doesn't seem to work.

How do I do this, especially if I'm already checking for a number of symbols to be present, like #, &, @, etc...

like image 854
KdgDev Avatar asked Dec 19 '09 15:12

KdgDev


1 Answers

Escaping that character is the right way. But make sure that you escape the escape characters in your string declaration as well:

"\\|"
'\\|'

Another way would be to use a character class ([|]) since in there you only have the characters ], ^ (only at the start), -, the escape character \ itself and the delimiter that have a special meaning and thus need to be escaped if meant to be expressed as plain characters.

like image 128
Gumbo Avatar answered Sep 29 '22 08:09

Gumbo