New Literal Forms in Nu

Wednesday October 17, 2007

Nu-0.2.0 contains support for two new literal types: regular expressions and characters.

Regular Expression Literals

The Nu parser now allows regular expressions to be specified with the following form:

/regex/options

Here "regex" is a regular expression and "options" is a string of lower-case alphabetic characters.

The available options correspond to the options available for the NuRegex class:

i case insensitive comparison
s dot metacharacter also matches newlines
x allow whitespaces and comments in pattern
l lazy quantifiers become greedy and vice versa
m caret and dollar anchors match at newlines

For example, /^f(o+)$/i matches strings "fo", "FOO", and "fooooOOOOO".

To include forward slashes in regular expressions, prefix each forward slash with a backslash.

Character Literals

Character literals are single characters or escape sequences enclosed in pairs of single quotes ('). The same escape sequences are supported for character literals and strings. Internally, a character literal is represented by an NSNumber with the appropriate unicode value. This is the same value and type that is returned by the NSString characterAtIndex: method.