Some Android Confectionery

Starting with the 1.5 version, Android releases have been given codenames consisting of names of sweets, candies, and other sugary foods. The platform version spanning from 4.1 to 4.3, for example, was codenamed Jelly Bean. And when version 4.4 came out, Google codenamed it KitKat (with Nestlé even launching their own promotion of Android Kit Kat bars with a chance to win a Nexus 7 tablet).

To make it sound more geeky, these confectionery codenames follow an alphabetical order. Some sites and forums are now predicting the names of future releases. I also heard Google built statues of these desserts in their heardquarters in Mountain View, California. Here’s the list:

Cupcake (Android 1.5)

Donut (Android 1.6)

Eclair (Android 2.0-2.1.x)

Froyo (Android 2.2.x)

Gingerbread (Android 2.3-2.3.x)

Honeycomb (Android 3.0-3.2)

Ice Cream Sandwich (Android 4.0.x)

Jelly Bean (Android 4.1-4.3)

KitKat (Android 4.4)

Lollipop (Android 5.0)


The Heartbleed Hit List

Heartbleed is a major bug in the OpenSSL library (an open-source implementation of the SSL protocol) that was reported on April 1 of this year. It has been existing for almost two years before being discovered. This link shows a list of some major Websites that were affected. Some sites were not affected, and those that were affected have fixed or updated their servers with a patch. Many of those have advised their users to change their passwords in case their accounts have been compromised before their servers were patched.


Regular Expressions

A regular expression, or regex, is a pattern of characters that can be matched against a string. It is useful for searching within text for one or more specific substrings that match the pattern. A typical use case I encounter a lot is when I need to search for a specific pattern in a huge file (possibly multiple files), and occasionally I need to replace all occurrences of this pattern with another string. Many editors support regular expressions for searching files. Most programming languages (e.g. Java, .NET, PHP) also have native support for regular expressions, although the syntax of the regex may vary from one language to another.



String Constants and Metacharacters

The most basic form of regex is a string constant that has no special characters. For example, the expression foo will match exactly the substring foo in food as shown in the below pseudocode.

"food".match("foo")  // "foo"

The following characters are interpreted as special characters: ([{\^$-|*+.?}])

To force using one of these characters as a normal character, a backslash (“\”) must be used.

"Value: 20.5".match("20\.5")  // "20.5"


Character Classes

There are some predefined character classes that can be used in regular expressions:

Type Description
. Any character except new line characters (\n and \r)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \r\n\t\f]
\S A non-whitespace character: [^ \r\n\t\f]
\w A word character: [a-zA-Z0-9_]
\W A non-word character: [^a-zA-Z0-9_]


"hamburger".match(".urger")  // "burger"

"abc1".match("ab.\d")  // "abc1"

"abc1".match(".\d")  // "c1"

"abc1n".match(".\d\D") // "c1n"

"abc123".match(".\d\D") // no match

"abc@123".match("\w\W\d") // "c@1"

The user can also specify the range or set of characters to be matched in the regex. For example, [agn] would match either a, g, or n. [a-g] would match any character between a and g, inclusive. On the other hand, [^aeiou] would match any character that is not a vowel.

"In 1991".match("19[0-9][0-9]")  // "1991"

"In 2003".match("200[^0-5]")     // no match

"In 2009".match("200[^0-5]")     // "2009"

The pipe character (|) is equivalent to a logical OR operator, causing the regex to match either the left side or the right side of the expression:

"Deep Purple".match("Blue|Purple")  // "Purple"



Quantifiers are used to repeat the pattern in the regular expression. The basic quantifiers are: ? * +
The user can also specify the number of occurrences of a pattern in a string using curly brackets.

Quantifier Description
x? x, zero or one occurrence
x* x, zero or more occurrences
x+ x, one or more occurrences
x{n} x, exactly n occurrences


"1-22-333".match("\d{3}")           // "333"

"1-22-333@".match("\d+-\d{3}@\d*")  // "22-333@"

"-333@".match("\d+-\d{3}@\d*")      // no match

"-333@7".match("-?\d{3}@")          // "-333@"

Greedy vs. Lazy Matching

There are two types of matching that can be done when using quantifiers: greedy or lazy. In most regex implementations, a match can be made lazy by adding a question mark to the quantifier as shown below.

"honolulu".match("ho.+lu")   // "honolulu" (greedy)

"honolulu".match("ho.+?lu")  // "honolu" (lazy)


Boundary Matchers

A boundary matcher specifies where in the string the pattern should be matched, for example at the start or end of the string. The below table lists some of these matchers:

Character Description
^ The start of a line
$ The end of a line
\A The start of the string (don’t care about new lines)
\Z The end of the string (ignore final line terminator, if any)
\z The end of the string
\b A word boundary
\B A non-word boundary
\< The start of a word (similar to \b, but only the start of the word
\> The end of a word (similar to \b, but only the end of the word


"guitar".match("^guitar$")        // "guitar"

"guitar  ".match("^guitar$")      // no match

"guitar  ".match("^guitar\s*")    // "guitar  "

"This is a guitar".match("\bguitar\b")     // "guitar"

"This is a guitarist".match("\bguitar\b")  // no match

"This is a guitar".match("\bguitar\B")     // no match

"This is a guitarist".match("\bguitar\B")  // "guitar"



A capturing group is used to group a substring within the matched pattern as a single unit. It can be referenced back within the regular expression. A group is marked by placing the characters inside parentheses, and is referenced by a number depending on the total number of groups in the regex. For example, in (\d+)-(\w+)@\d-\1, (\d)+ is the first group and (\w+) is the second group. The first group is also referenced at the end of the regex.

"d1d1".match("(\w\d)\1")    // "d1d1"

"d1d2".match("(\w\d)\1")    // no match