Backslash in Regex- PHP -
I'm trying to learn Reggae in PHP and now I'm stuck here. My questions may look silly, but to explain pls.
I went through a link:
But I could not understand:
In response, he mentioned two statements:
2 backslashes are used for not leaving a string ( "\\\\" ->
\\ )
1 backslash Regex is not used to leave the engine ( \\ ->
\ )
My question:
what is the word Does "not leave" really mean it? What is the purpose of unacking? Why do we need 4 backslashes to be included in regex?
itemprop = "text"> Backslash does not have a special meaning in both Regexen and PHP in both cases It is used as an Escape character. For example, if you want to type literal quotation character within a PHP string, then it will not work:
$ str = '' '; PHP is "confused" which ends with the ' string and which is the part of the string. That is where \ comes in:
$ str = '\' '; exits ' has special meaning, therefore instead the string is literally finished, now it's just a normal character string. There are also more Escape series like \ n .
This means that \ is a special character with special meaning. To avoid this puzzle when you want to write a literal \ , you will have to protect the literal backslash from \\ :
$ Str = '\\'; // string literally represents a backslash This works similar to both PHP and regexen. If you want to write a literal backslash in a regedx, you will see the / \ / . Now, since you are writing your regexen as a PHP string, you have to save it:
$ regex = '/ \\\\ /'; A pair of \\ becomes the first in a \ PHP string escaping mechanism, so the actual regex / \\ / , which is a regex which means "a backslash"
Comments
Post a Comment