Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PCRE regex match a null character?

Tags:

regex

null

pcre

I have a text source with nulls in it and I need to pull them out along with my regex pattern. Can regex even match a null character?

I only realized I had them when my pattern refused to match and when I pasted it into Notepad++ it showed all the null characters.

like image 723
Keng Avatar asked Apr 05 '10 20:04

Keng


People also ask

What is PCRE matching?

PCRE tries to match Perl syntax and semantics as closely as it can. PCRE also supports some alternative regular expression syntax (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, .

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Does regex match anything?

Matching a Single Character Using Regex ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.

How do I match a character in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .


3 Answers

\x00 

That is a null char.

like image 156
aaaaaaaaaaaa Avatar answered Sep 28 '22 00:09

aaaaaaaaaaaa


One issue with matching the null character is that you first need to arrange to have it arrive. Lots of languages use null-terminated strings so your match may not be against the entire input.

As for how to express it in PCRE, \000 works and is not going to get tripped up by anything following it, as would \x{} (but the octal version is in my opinion easier to identify when skimming the regex).

See the PCRE manpages and search for Non-printing characters for the full details of how to specify a null in various different ways.

like image 20
ChrisF Avatar answered Sep 27 '22 23:09

ChrisF


To clarify/add another detail to previous answer: PCRE library accepts pattern as a "C" nul-terminated string. (Quoting PCRE docs: "The pattern is a C string terminated by a binary zero".) That means that pattern cannot contain a literal NUL character - instead, it must be always escaped using means described in other answers. ("Unlike the pattern string, the subject may contain binary zeroes." " 4. Though binary zero characters are supported in the subject string, they are not allowed in a pattern string because it is passed as a nor- mal C string, terminated by zero. The escape sequence \0 can be used in the pattern to represent a binary zero.")

NUL character is the only character in PCRE pattern which must be escaped, all other may go literal: "There is no restriction on the appearance of non-printing characters, apart from the binary zero that terminates a pattern".

As a final comparative note, some other Perl-compatible regex engines do allow literal NULs in a pattern, for example, Python's SRE. E.g. urlib.parse from Python3 has following line: _asciire = re.compile('([\x00-\x7f]+)'). Note the lack of "r" to signify raw literal - it means that unescaping here happens on Python level, and re module gets characters with values 0x00 and 0x7f in pattern.

like image 44
pfalcon Avatar answered Sep 27 '22 22:09

pfalcon