Ignite-UX Custom Configuration files
This
example is not a reference on how to write regular expressions (complex regular expression
writing can be very difficult) but we will look at some common operators and how you might use
them. The information provided here is only a very basic introduction to extended regular
expressions.
An important thing to remember is that
~ is the operator that controls regular expression matching
within Ignite-UX configuration files. When writing regular expressions there are several operators
you might find useful
67
:
“^” is an anchor to the start of what is being matched against. This might allow you to say
something like
“^u” which would match anything that started with a “u”. Without the “^”,
a regular expression that just contained
“u” would match any occurrence of “u” in what
was being matched against.
“$” is an anchor to the end of what is being matched against. This might allow you to say
something like
“12$” which would match anything that ended with “12”.
“|” in a regular expression acts as an “or” operator. This allows you to give several
regular expressions together where either expression is allowed to match. For example
“^user|^sysadm” would match anything that started with “user” or “sysadm”.
“.” allows you to say any character. For example, “u.s” would match anything that
contained (no anchor is in the expression) the letter
“u” followed by any character
followed by the letter
“s”.
“[]” forms a bracket expression in a regular expression. This construct can be used to give
multiple values that can be used to match a single character. For example
“[ahj]” means
anything that contains the letters
“a”, “h” or “j”.
You can give a character class in the regular expression, for example
“[:alpha:]” can
be used to match any letter (upper or lowercase). The manual page regexp(5) contains
more information on character classes (including the names of other character classes.)
Note that you must use this within a bracket expression
68
so it will end up looking like
“[[:alpha:]]”.
“*” can be used to perform wildcard matching, the regular expression “u*r” matches
anything containing a
“u” followed by zero or more characters followed by an “r”. For
example, this would match
“ur”, “usr”, and “user” (and many other things.)
Do not use the construct
“.*” (zero or more of any character) at the start or end of an
unanchored regular expression, it is pointless. For example
“.*user.*” means zero or
more of any character followed by
“user” followed by zero or more of any character. The
reason why this is pointless is that
“user” by itself will match any part of the string it is
being compared against. If compared against the string
“auser” it will match without any
additional “.*” in the regular expression.
“+” Allows you to match one or more occurrences of a pattern within an expression.
Note that the meaning of any of the regular expression operators mentioned above can change
depending on their position. An example of this would be
“|” inside a bracket expression: inside a
bracket expression it means the character “
|” not the “or” operator. Please refer to the regexp(5)
manual page for additional information.
Now we can look at the use of regular expressions for adding some sanity checks against the user
name from Example 5:
67
Some of these explanations have been simplified; you should consult the regexp(5) manual page for more complete explainations.
68
Outside of a bracket expression [:alpha:] in a regular expression would mean match the characters “:alpha” but [[:alpha:]] means match
any upper or lower case letter.
144