May 2009
Intermediate to advanced
510 pages
15h
English
You want to find binary numbers in a larger body of text, or check whether a string variable holds a binary number.
Find a binary number in a larger body of text:
\b[01]+\b
| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Check whether a text string holds just a binary number:
\A[01]+\Z
| Regex options: None |
| Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby |
^[01]+$
| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python |
Find a binary number with a B suffix:
\b[01]+B\b
| Regex options: Case insensitive |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Find a binary byte value or 8-bit number:
\b[01]{8}\b| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Find a binary word value or 16-bit number:
\b[01]{16}\b| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Find a string of bytes (i.e., a multiple of eight bits):
\b(?:[01]{8})+\b| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
All these regexes use techniques explained in the previous two
recipes. The key difference is that each digit is now a 0 or a 1. We easily match that
with a character class that includes just those two characters:
‹[01]›.
Read now
Unlock full access