Next: , Previous: , Up: Top   [Contents][Index]

6 Patterns

The patterns in the input (see Rules Section) are written using an extended set of regular expressions. These are:


match the character ’x’


any character (byte) except newline


a character class; in this case, the pattern matches either an ’x’, a ’y’, or a ’z’


a "character class" with a range in it; matches an ’a’, a ’b’, any letter from ’j’ through ’o’, or a ’Z’


a "negated character class", i.e., any character but those in the class. In this case, any character EXCEPT an uppercase letter.


any character EXCEPT an uppercase letter or a newline


the lowercase consonants


zero or more r’s, where r is any regular expression


one or more r’s


zero or one r’s (that is, “an optional r”)


anywhere from two to five r’s


two or more r’s


exactly 4 r’s


the expansion of the ‘name’ definition (see Format).


the literal string: ‘[xyz]"foo


if X is ‘a’, ‘b’, ‘f’, ‘n’, ‘r’, ‘t’, or ‘v’, then the ANSI-C interpretation of ‘\x’. Otherwise, a literal ‘X’ (used to escape operators such as ‘*’)


a NUL character (ASCII code 0)


the character with octal value 123


the character with hexadecimal value 2a


match an ‘r’; parentheses are used to override precedence (see below)


apply option ‘r’ and omit option ‘s’ while interpreting pattern. Options may be zero or more of the characters ‘i’, ‘s’, or ‘x’.

i’ means case-insensitive. ‘-i’ means case-sensitive.

s’ alters the meaning of the ‘.’ syntax to match any single byte whatsoever. ‘-s’ alters the meaning of ‘.’ to match any byte except ‘\n’.

x’ ignores comments and whitespace in patterns. Whitespace is ignored unless it is backslash-escaped, contained within ‘""’s, or appears inside a character class.

The following are all valid:

(?:foo)         same as  (foo)
(?i:ab7)        same as  ([aA][bB]7)
(?-i:ab)        same as  (ab)
(?s:.)          same as  [\x00-\xFF]
(?-s:.)         same as  [^\n]
(?ix-s: a . b)  same as  ([Aa][^\n][bB])
(?x:a  b)       same as  ("ab")
(?x:a\ b)       same as  ("a b")
(?x:a" "b)      same as  ("a b")
(?x:a[ ]b)      same as  ("a b")
    /* comment */
    c)          same as  (abc)
(?# comment )

omit everything within ‘()’. The first ‘)’ character encountered ends the pattern. It is not possible to for the comment to contain a ‘)’ character. The comment may span lines.


the regular expression ‘r’ followed by the regular expression ‘s’; called concatenation


either an ‘r’ or an ‘s


an ‘r’ but only if it is followed by an ‘s’. The text matched by ‘s’ is included when determining whether this rule is the longest match, but is then returned to the input before the action is executed. So the action only sees the text matched by ‘r’. This type of pattern is called trailing context. (There are some combinations of ‘r/s’ that flex cannot match correctly. See Limitations, regarding dangerous trailing context.)


an ‘r’, but only at the beginning of a line (i.e., when just starting to scan, or right after a newline has been scanned).


an ‘r’, but only at the end of a line (i.e., just before a newline). Equivalent to ‘r/\n’.

Note that flex’s notion of “newline” is exactly whatever the C compiler used to compile flex interprets ‘\n’ as; in particular, on some DOS systems you must either filter out ‘\r’s in the input yourself, or explicitly use ‘r/\r\n’ for ‘r$’.


an ‘r’, but only in start condition s (see Start Conditions for discussion of start conditions).


same, but in any of start conditions s1, s2, or s3.


an ‘r’ in any start condition, even an exclusive one.


an end-of-file.


an end-of-file when in start condition s1 or s2

Note that inside of a character class, all regular expression operators lose their special meaning except escape (‘\’) and the character class operators, ‘-’, ‘]]’, and, at the beginning of the class, ‘^’.

The regular expressions listed above are grouped according to precedence, from highest precedence at the top to lowest at the bottom. Those grouped together have equal precedence (see special note on the precedence of the repeat operator, ‘{}’, under the documentation for the ‘--posix’ POSIX compliance option). For example,


is the same as


since the ‘*’ operator has higher precedence than concatenation, and concatenation higher than alternation (‘|’). This pattern therefore matches either the string ‘fooor the string ‘ba’ followed by zero-or-more ‘r’’s. To match ‘foo’ or zero-or-more repetitions of the string ‘bar’, use:


And to match a sequence of zero or more repetitions of ‘foo’ and ‘bar’:


In addition to characters and ranges of characters, character classes can also contain character class expressions. These are expressions enclosed inside ‘[:’ and ‘:]’ delimiters (which themselves must appear between the ‘[’ and ‘]’ of the character class. Other elements may occur inside the character class, too). The valid expressions are:

    [:alnum:] [:alpha:] [:blank:]
    [:cntrl:] [:digit:] [:graph:]
    [:lower:] [:print:] [:punct:]
    [:space:] [:upper:] [:xdigit:]

These expressions all designate a set of characters equivalent to the corresponding standard C isXXX function. For example, ‘[:alnum:]’ designates those characters for which isalnum() returns true - i.e., any alphabetic or numeric character. Some systems don’t provide isblank(), so flex defines ‘[:blank:]’ as a blank or a tab.

For example, the following character classes are all equivalent:


A word of caution. Character classes are expanded immediately when seen in the flex input. This means the character classes are sensitive to the locale in which flex is executed, and the resulting scanner will not be sensitive to the runtime locale. This may or may not be desirable.

Next: , Previous: , Up: Top   [Contents][Index]