strmatch/3—match a string with a simple pattern and retrieve fields from it
The strmatch/3 function matches a string with a simple pattern and retrieve fields from it. The syntax for this function is as follows:
$FLDS=strmatch($STR,$PAT)
strmatch/3 arguments
Argument | Mode | Type | Description |
---|---|---|---|
$STR | Input | STRING | Specifies the string with which the pattern is to be matched |
$PAT | Input | STRING | Specifies the pattern to match with the string |
$FLDS | Output | LIST_OF STRING | Retrieved fields |
Use the strmatch/3 function to match a string $STR with a pattern $PAT and retrieve fields from it in $FLDS.
The pattern $PAT consists of literal text and value substitutes. Literal text is matched literally. Space characters in the pattern are matched with any number of consecutive spaces. Non-printable or special characters can be specified in the text with escape sequences:
* | Backslash |
\s | Space (single space) |
\n | New line |
\r | Carriage return |
\t | Tab |
\0ddd | Character code in octal |
A substitute is preceded by a % sign, followed by a type indicator. Between the % and the type indicator, an optional * suppression modifier can be added. The values corresponding to the substitutes are collected in the fields in $FLDS, in order of appearance. Suppressed substitutes are matched, but the values are not collected.
Possible substitutes are:
%% | Lliteral match of % |
%d | Decimal integer number |
%f | Floating point real number |
%c | Single character |
%s | String value |
Two substitutes cannot occur without literal text in between them. The portion of the input that matches the substitute of %s depends on the pattern following the substitute:
- A literal: Input up to the first literal
- A space and a literal: Input up to the space followed by the literal
- A space and a substitute: Input up to the first space
The $FLDS argument can be specified as one variable that will get a list value or it can be specified as a list of as many variables as there are fields specified in the pattern.
strmatch/3 example
The pattern defines two fields, one string and one integer number, separated with spaces. The variable $FLD1 will get the value abc def and variable $FLD2 will get the value 12.
The pattern defines one string field, preceded by the text Hi and followed by an ! (exclamation point). The variable $FLDS will be set to a list containing the single string you there.
This call will fail because the pattern defines one string field, preceded by the text Hi and followed by an ! (exclamation point), but the input string does not contain an ! (exclamation point) at the end.
The pattern defines two string fields, separated with spaces and followed by text. The variable $FLD1 will get the value a, because the first string substitute is followed by a space and a substitute which consumes the input up to the first space. The variable $FLD2 will get the value b c because its string substitute is followed by a space and a literal which consumes the input up to the matching literal.