Skip navigation.

Command Reference

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents View as PDF   Get Adobe Reader




rex—Offline regular expression compiler and tester.



rex pattern_file C_file 


rex pattern [file . . . ]


When invoked without arguments, rex reads regular expressions from the standard input and writes initialized character arrays to the standard output. Normally, the output is included in a C program. This saves on both execution time and program size. The command rex compiles the regular expressions on the standard input (normally redirected from an input file) and writes the output to the standard output (normally redirected to an output file).

The input file may contain several patterns, each with the following form:

name string [string . . .]

Here name is the C name to be used for the output array and string is the regular expression enclosed with double quotes. Where more than one string follows a name they are concatenated into one string. (Multiple strings are strictly a formatting convenience.) If double quotes occur in the pattern they need to be preceded by a backslash.

The output may be included in a C program or compiled and later loaded. In the C program that uses the rex output, rematch(abc,line,0) applies the regular expression named abc to line.

The following is a sample input file:

cname   "[a-zA-Z_][a-(3c)-Z0-9_]*" 

tn "\\\\(([0-9]{3})$0\\\\)"

The following is the corresponding output:

/* pattern: "[a-aA-Z_][a-zA-Z0-9_]*" */ 
char cname[] = {
... };

/* pattern: "\\\\(([0-9]{3})$0\\\\)([0-9]{3})$1-([0-9]{4})$2" */
char tn[] = {
... };

rex can be used to try patterns against test data by invoking it with one or more arguments. The first argument is taken as a pattern (regular expression) to be applied to each line of the files whose names are mentioned in the remaining arguments. If no filename arguments are given the standard input is used. The special filename, -, may be used as an argument to refer to the standard input.

When matching text is found, the line containing the match is printed and the matching portion of the line is underlined. In addition, any text extracted for specified sub-patterns is printed on separate line(s).

For example, the command:

rex '(^| )([0-9]+)$0(|$)'

with input:

... or 200 programmers in one week.
This sentense has 3 erors.
I need 12 bad men.


... or 200 programmers in one week.
$0 = \Q200'

This sentense has 3 erors.
$0 = \Q3'

I need 12 bad men.
$0 = \Q12'


Errors include file open errors, argument errors, and so on, and are self-explanatory.

See Also

regular expression information in tpsubscribe(3c)


Skip navigation bar  Back to Top Previous Next