Syntax
|
a. if <string>=~<pattern> b. var <objname>={<string>}X{<pattern>}:{pname1,..pnameN} // compare or extract sub string from a string
|
Text
|
Caravan language has good support for pattern matching. There are two statements that can be used to test a string against a given pattern. We will first explain the syntax of the two statements before explaining the pattern rules.
1. Check if a string matches with pattern/s in an if statement
[else]if <string>=~<pattern>
where string is a variable expression pattern is a quoted string or variable expression
example 1 if myfile(filename)=~"`*`.pdf" .. endif
example 2. var mypattern mypattern(val)="*.cpp" mypattern(val)="*.h" if myfile(filename)=~mypattern(val) ... endif
In the above example if mypattern(val) has multiple values it will check for a match against any one of them.
2. Check if a string matches a pattern and create an object by capturing the substrings:
var <objname>={<string>}X{<pattern>}:{pname1,..pnameN}
The above statement creates an object of name 'objname' if the 'string' matches the 'pattern' and captures the required pname1 .. pnameN fields from the string ; The object is not created if the string does not match.
example
var x={"test.data 1230 bytes"}X{"`*`%`*`%*"}:{filename,size}
The " ` " symbol is used to demarcate the substring which is to be captured. The first substring thus captured is assigned to the first propertyname in the namelist and so on. In the pattern " ` " is invisible to the pattern matching algorithm.
Here the object x will be created with two properties x(filename);// "test.data" x(size);// "1230"
Here again the 'string' and 'pattern' can be a variable expression , a quantity which is generated at runtime. Unlike in the previous case, only one pattern and one string is expected.
More about patterns:
A pattern is a string in which some symbols are used to denote some rules much like the wild card characters used in filenames. For example "*.*" matches any string which has '.' in it. Similarly "?.*" matches string with one and only character before '.'.
In caravan the patterns can be constructed to parse very complex strings.
Pattern matching symbols and their usage are shown as properties.
|
Sample
|
The following are two strings which is given by the "dir" command which we will parse using the script given below.
" 3-02-99 5:56p 252 0 wireserver.wpj" "12-07-98 9:02p 1060 0 wiretest.cpp"
Code which you can test by copying into a .html file in the template directory:
<caravan> // Example: var string string(val)=" 3-02-99 5:56p 252 0 wireserver.wpj" string(val)="12-07-98 9:02p 1060 0 wiretest.cpp"
loop sl (string(val(00))); // start loop "sl"
"Test String : ";string(val(sl(count)));"<hr>"
var x= {string(val(sl(count)))}X{"~%`#~#`-`##`-`##`~%`#~#`:`##``?`~%`#~#`~%`#~#`~%`*`"}:{date,month,year,hour,minute,ampm,size,ea,name}
// parse each string and create an output if x "Pattern has matched: Following is the reformatted string<br>" x(name);" ";x(size);" ";x(month);" ";"/";" ";x(date);" ";"/";" ";x(year);" " ";x(hour);":";x(minute) if x(ampm)="a" "AM" elseif x(ampm)="p" "PM" endif else "Pattern has not matched<br>" endif repeat sl 16;// end loop "sl"
// Example: //Pattern matching in if statement // if <string>=~<pattern> if myfile(filename)=~"`*`.pdf" if myfile(filename)=~mypattern(val)
//Example: //Check if a string matches a pattern and create an object by capturing the substrings: //var <objname>={<string>}X{<pattern>}:{pname1,..pnameN} //ex: var x={"test.data 1230 bytes"}X{"`*`%`*`%*"}:{filename,size} x(filename);// "test.data" x(size);// "1230"
//ex: var xt xt(from)="04-12-2003 10:00" var hm={xt(from)}X{"`*`-`*`-`*`%~%`*:*`"}:{date,month,year,hr} ; // extract data. Data between ` ` is captured and assigned to the respective variables. hm(date) ; // will be 04 hm(month) ; // will be 12 hm(year) ; // will be 2003 hm(hr) ; // will be 10:00
var mm={hm(hr)}X{"`*`:`*`"}:{hr,mn} </caravan>
|
Properties
|
*
?
%
$
#
@
&
^
~
`
\
|
Home
Back
|
|