HELP
HELP

Reputation: 45

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

Upvotes: 1

Views: 369

Answers (3)

Felix Kling
Felix Kling

Reputation: 816262

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:

  • For octal: a slash \ followed by one to three digits between 0 and 7.
  • For hex: a slash \ followed by x followed by one or two characters which can be either digits or upper or lower case letters.

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";

Upvotes: 4

fingerman
fingerman

Reputation: 2470

there is a problem with your regex, it won't test a string properly because you don't have ^ in the beginning and $ in the end, but never mind that.

ok, the first one is

match range 0-7 one 1 to 3 charcters

second one is

match range 0-9 AND A-F (capital) AND a-f(small) 1 to 2 charcters 

The x in the second one is probably a mistake...

Upvotes: 0

Edoardo Pirovano
Edoardo Pirovano

Reputation: 8334

Those two regular expressions define the way the number should be formatted. The [0-7] means to allow all digits between zero and seven, and the {1,3} after it means that that there may be between one and three of those digits.

Similarly in the second regular expression, [0-9A-Fa-f] means all the numbers between zero and nine, all the uppercase letters between A and F, and all the lowercase letters between a and f. The {1,2} means that there must be one or two of those digits/letters.

Upvotes: 2

Related Questions