Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When testing an answer for another user's question I found something I don't understand. The problem was to replace all literal characters from a string with a single space.

Now, the first pattern I tried was:

/(?:\[trn])+/

which surprisingly didn't work. I tried the same pattern in Perl and it worked fine. After some trial and error I found that PHP wants 3 or 4 backslashes for that pattern to match, as in:

/(?:\\[trn])+/

or

/(?:\[trn])+/

these patterns - to my surprise - both work. Why are these extra backslashes necessary?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
582 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need 4 backslashes to represent 1 in regex because:

  • 2 backslashes are used for unescaping in a string ("" -> \)
  • 1 backslash is used for unescaping in the regex engine (\ -> )

From the PHP doc,

escaping any other character will result in the backslash being printed too1

Hence for \[,

  • 1 backslash is used for unescaping the , one stay because [ is invalid ("\[" -> \[)
  • 1 backslash is used for unescaping in the regex engine (\[ -> [)

Yes it works, but not a good practice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...