Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

Right way to escape backslash [ ] in PHP regex?

Just out of curiosity, I'm trying to figure out which exactly is the right way to escape a backslash for use in a PHP regular expression pattern like so:

TEST 01: (3 backslashes)

$pattern = "/^[\]{1,}$/";
$string = '\';

// ----- RETURNS A MATCH -----

TEST 02: (4 backslashes)

$pattern = "/^[\\]{1,}$/";
$string = '\';

// ----- ALSO RETURNS A MATCH -----

According to the articles below, 4 is supposedly the right way but what confuses me is that both tests returned a match. If both are right, then is 4 the preferred way?

RESOURCES:

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)
// PHP 5.4.1

// Either three or four  can be used to match a ''.
echo preg_match( '/\/', '\' );        // 1
echo preg_match( '/\\/', '\' );       // 1

// Match two backslashes `\`.
echo preg_match( '/\\\/', '\\' );   // Warning: No ending delimiter '/' found
echo preg_match( '/\\\/', '\\' );  // 1
echo preg_match( '/\\\\/', '\\' ); // 1

// Match one backslash using a character class.
echo preg_match( '/[\]/', '\' );       // 0
echo preg_match( '/[\]/', '\' );      // 1  
echo preg_match( '/[\\]/', '\' );     // 1

When using three backslashes to match a '' the pattern below is interpreted as match a '' followed by an 's'.

echo preg_match( '/\\s/', '\ ' );    // 0  
echo preg_match( '/\\s/', '\s' );    // 1  

When using four backslashes to match a '' the pattern below is interpreted as match a '' followed by a space character.

echo preg_match( '/\\s/', '\ ' );   // 1
echo preg_match( '/\\s/', '\s' );   // 0

The same applies if inside a character class.

echo preg_match( '/[\\s]/', ' ' );   // 0 
echo preg_match( '/[\\s]/', ' ' );  // 1 

None of the above results are affected by enclosing the strings in double instead of single quotes.

Conclusions:
Whether inside or outside a bracketed character class, a literal backslash can be matched using just three backslashes '\' unless the next character in the pattern is also backslashed, in which case the literal backslash must be matched using four backslashes.

Recommendation:
Always use four backslashes '\\' in a regex pattern when seeking to match a backslash.

Escape sequences.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...