Pattern Matching : 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 =~ 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 ={}X{}:{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 is illustrated below: * ; matches any character multiple times example: *ing = "walking", "running" etc. ? ; matches any character once ?ut = "cut", "put", "but" etc.. %; matches space ' ' and tab *%*="hello world", "this is string", "1. Bombay" etc. $ ; matches any lowercase $*="abcd", "a" etc. # ; matches any digit #.## = "1.23","1.66" etc. @ ; matches any upper case @@@ = "GOD" & ; matches any alphabet &&& = "God" ^ ; matches white space including line feeds *^="hello world\n" ~ ; matches next chararacter or symbol multiple times ~%@~$ = "World", " World" etc. ` ; marks the starting and ending of substring which is to be captured; ~%`@~$` ="World", " World" and captures "World" \ ; escapes next character from being treated as a special symbol \~* ="~anything", \###="#12" Other characters are matched exactly (not case sensitive) "A*" = "apple", "Apple" etc. Now we will show an example of how to use the pattern matching and parsing capabilities of caravan. 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: 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)));"
" 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
" "" "
" 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
" endif repeat sl 16;// end loop "sl"
Please comment.