phpregexhexoctal

Explain these two regular expressions (octal notation and hexadecimal notation)


I know this is a stupid question but I want to know what the meaning means in simple terms for each sequence below.

\[0-7]{1,3} 

the sequence of characters matching the regular expression is a character in octal notation

\x[0-9A-Fa-f]{1,2}

the sequence of characters matching the regular expression is a character in hexadecimal notation


Solution

  • It means that if you have a string like "foo bar \041", \041 will be treated as octal representation of a character. Similar for the hexadecimal sequence.

    The regular expressions define the structure the character sequences have to follow in order to be interpreted as octal or hex representation:

    Have a look at the ASCII table to see each character's octal and hexadecimal equivalent.

    For example:

    echo "\064\062"; // echos 42
    

    In hex:

    echo "\x52\x50";