1regex(1F)                        FMLI Commands                       regex(1F)
2
3
4

NAME

6       regex - match patterns against a string
7

SYNOPSIS

9       regex [-e] [-v "string"] [pattern template] ...
10            pattern [template]
11
12

DESCRIPTION

14       The regex command takes a string from the standard input, and a list of
15       pattern / template pairs,  and  runs  regex()  to  compare  the  string
16       against each pattern until there is a match. When a match occurs, regex
17       writes the corresponding template to the standard  output  and  returns
18       TRUE.  The  last (or only) pattern does not need a template. If that is
19       the pattern that matches the string, the function simply returns  TRUE.
20       If no match is found, regex returns FALSE.
21
22
23       The  argument  pattern is a regular expression of the form described in
24       regex(). In most cases, pattern should be enclosed in single quotes  to
25       turn  off special meanings of characters. Note that only the final pat‐
26       tern in the list may lack a template.
27
28
29       The argument template may contain the strings $m0  through  $m9,  which
30       will be expanded to the part of pattern enclosed in ( ... )$0 through (
31       ... )$9 constructs (see examples below). Note that if you use this fea‐
32       ture,  you  must  be  sure to enclose template in single quotes so that
33       FMLI does not expand $m0 through $m9 at parse time. This feature  gives
34       regex  much  of the power of cut(1), paste(1), and grep(1), and some of
35       the capabilities of sed(1). If there is no  template,  the  default  is
36       $m0$m1$m2$m3$m4$m5$m6$m7$m8$m9.
37

OPTIONS

39       The following options are supported:
40
41       -e             Evaluates  the  corresponding  template  and  writes the
42                      result to the standard output.
43
44
45       -v "string"    Uses string instead  of  the  standard  input  to  match
46                      against patterns.
47
48

EXAMPLES

50       Example 1 Cutting letters out of a string
51
52
53       To  cut  the 4th through 8th letters out of a string (this example will
54       output strin and return TRUE):
55
56
57         `regex -v "my string is nice" '^.{3}(.{5})$0' '$m0'`
58
59
60
61       Example 2 Validating input in a form
62
63
64       In a form, to validate input to field 5 as an integer:
65
66
67         valid=`regex -v "$F5" '^[0-9]+$'`
68
69
70
71       Example 3 Translating an environment variable in a form
72
73
74       In a form, to translate an environment variable which contains  one  of
75       the numbers 1, 2, 3, 4, 5 to the letters a, b, c, d, e:
76
77
78         value=`regex -v "$VAR1" 1 a 2 b 3 c 4 d 5 e '.*' 'Error'`
79
80
81
82
83       Note the use of the pattern '.*' to mean "anything else".
84
85
86       Example 4 Using backquoted expressions
87
88
89       In  the  example  below, all three lines constitute a single backquoted
90       expression. This expression, by itself, could be put in a menu  defini‐
91       tion  file.  Since  backquoted  expressions  are  expanded  as they are
92       parsed, and output from a backquoted expression (the  cat  command,  in
93       this  example)  becomes  part of the definition file being parsed, this
94       expression would read /etc/passwd and make a dynamic menu  of  all  the
95       login ids on the system.
96
97
98         `cat /etc/passwd | regex '^([^:]*)$0.*$' '
99         name=$m0
100         action=`message "$m0 is a user"`'`
101
102
103

DIAGNOSTICS

105       If none of the patterns match, regex returns FALSE, otherwise TRUE.
106

NOTES

108       Patterns  and templates must often be enclosed in single quotes to turn
109       off the special meanings of characters. Especially if you use  the  $m0
110       through $m9 variables in the template, since FMLI will expand the vari‐
111       ables (usually to "") before regex even sees them.
112
113
114       Single characters in character  classes  (inside  [])  must  be  listed
115       before  character  ranges,  otherwise  they will not be recognized. For
116       example, [a-zA-Z_/] will not find underscores (_) or slashes  (/),  but
117       [_/a-zA-Z] will.
118
119
120       The  regular  expressions accepted by regcmp differ slightly from other
121       utilities (that is, sed, grep, awk, ed, and so forth).
122
123
124       regex with the -e option forces subsequent commands to be  ignored.  In
125       other words, if a backquoted statement appears as follows:
126
127         `regex -e ...; command1; command2`
128
129
130
131
132       command1  and  command2  would never be executed. However, dividing the
133       expression into two:
134
135         `regex -e ...``command1; command2`
136
137
138
139
140       would yield the desired result.
141

ATTRIBUTES

143       See attributes(5) for descriptions of the following attributes:
144
145
146
147
148       ┌─────────────────────────────┬─────────────────────────────┐
149       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
150       ├─────────────────────────────┼─────────────────────────────┤
151       │Availability                 │SUNWcsu                      │
152       └─────────────────────────────┴─────────────────────────────┘
153

SEE ALSO

155       awk(1), cut(1), grep(1), paste(1), sed(1), regcmp(3C), attributes(5)
156
157
158
159SunOS 5.11                        12 Jul 1999                        regex(1F)
Impressum