regexsearch, regexsubstitute, regexextract − RWP*Load Simulator regular expression statements
regexsearch ::= regex concatenation , concatenation , identifier { , identifier } regexsubstitute ::= regexsub concatenation , concatenation , concatenation , identifier | regexsubg concatenation , concatenation , concatenation , identifier regexextract ::= regexextract concatenation , concatenation , identifier { , identifier }
The regex search statement searches for one or more appearances of a regular expression in a concatenation, and saves matches into variables.
The regex substitute statements search for a regular expression in a concatenation and replaces the first or all occurrences of it by a replacement concatenation returning the result in a variable.
The regex extract statement searches for a regular expression in a concatenation and extracts parenthesized sub-expressions into variables.
These functions all use extended regular expressions (modern in POSIX terminology) and are implemented using regex(3) with cflags set to REG_EXTENED at the call to regcomp(3). The first statement is similar to egrep(1), the two substitute statements are similar to the s command in sed(1).
regex r,s,v1 [,v2 ...]
The value, r, must be an extended regular expression, which will be used to search s. The first, second, etc occurrence of the regular expression r in s will be saved in the identified variables. If there are no matches, the first (and compulsory) variable will be null. If there are fewer matches than variables, those not filled will be null. The variables can be of type integer, double or string, and even a variable of string will be null if no match is assigned to it.
regexsub r,s,n,v
regexsubg r,s,n,v
These statements require three concatenations that are string values, r, s and n, and one identifier, v. The extended regular expression, r, will be used to search s, in which any occurrence of r will be substituted by n. The identifier, v, must be a string variable and the result of the substitution will be saved in this variable. If there is no match, the variable will be set to null. In the replacement string, n, \N where N is a single digit refers to the N’th parenthesized sub-expression in the regular expression, r; see regex(3) for details. Note that if you code n as a string constant, the single \ must be escaped and therefore written \\. The difference between regexsub and regexsubg is that the former only replaces the first occurrence of r in s by n, the latter replaces all.
regexextract r,s,v1 [,v2 ...]
The value, r, must be an extended regular expression including parenthesized sub-expression, which will be used to search s. Each parenthesized sub-expression will be saved in the identified variables. If there is no match, the first (and compulsory) variable will be null. If there are fewer parenthesized sub-expressions than variables, those not filled will be null and a warning will be printed. The variables can be of type integer, double or string, and even a variable of string will be null if no match is assigned to it. If a sub-expression that is empty or only contains white space characters is assigned to a double or integer variable, the variable will be set to null.
The first example shows how the regex statement can be used to extract integers from a string. The output will be the values 12 and 34.
integer a,b; regex "[0-9]+", "ab12cd34ef56", a,b; printline a,b;
When executing this second example
string res; # replace all o by O regexsubg "o", "hello world","O",res; printline res; # reverse two words consisting of a-z regexsub "([a-z]+) ([a-z]+)", "hello world", "\\2 \\1",res; printline res;
the output will be these two lines:
hellO wOrld world hello
The last example shows how regexextract can be used to extract two white-space delimited words from a string and print them in the opposite order with a - between them:
string s,t; regexextract "([a-z]+)[ \t]+([a-z]+)", "!hello world42", s,t; printline t "-" s;
which will produce this output:
world-hello
Copyright
© 2023 Oracle Corporation
Licensed under the Universal Permissive License v 1.0 as
shown at https://oss.oracle.com/licenses/upl
statement(1rwl), variable(1rwl), concatenation(1rwl)