tags:

views:

82

answers:

1

Is it possible to use a three digit ASCII code in a PCRE regex? A space character is a two digit one (40). You need to add a zero and escape it like so: \040

+3  A: 

Yes, you can easily use octal escapes up to \377 in normal and \777 in UTF-8 mode. To quote from the manpage:

Non-printing characters

A second use of backslash provides a way of encoding non-printing char- acters in patterns in a visible manner. There is no restriction on the appearance of non-printing characters, apart from the binary zero that terminates a pattern, but when a pattern is being prepared by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents:

...
\ddd        character with octal code ddd, or backreference
\xhh        character with hex code hh
\x{hhh..}   character with hex code hhh..

I've included the hexadecimal escapes in this quote because they're quite likely easier to read and don't clash with backreferences. So depending on how often you use \123 it might be more sensible to resort to \x53.

Joey
Thank you for your help Johannes.
lanmind
@lanmind: If you want to increase the chance that other questions get answered, you should start accepting answers by pressing the green tick-button at the left. This marks the question as answered correctly. @Johannes: +1 for a great answer with simplicity and clarity
Atmocreations