next up previous contents index
Next: Note: Up: perlmatch: Using Perl as Previous: perlmatch: Using Perl as   Contents   Index

Iterative Pattern Matching

There are two ways to do matching. One is to first do the matching operation and then count the beans. To find the first match, do:

:- try_match( +String, +Pattern ).

Both arguments must be of the XSB string data types. If there is a match in the string, the submatches $1, $2, etc., the prematch substring $` (i.e., the part before the match), the postmatch substring $' (i.e., the part after the match), the whole matched substring $&, and the last parentheses match substring $+ will be stored into a global data structure, and the predicate try_match(string, pattern) succeeds. If no match is found, this predicate fails.

The ability to return parts of the match using the Perl variables $+, $1, $2, etc., is an extremely powerful feature of Perl. As we said, a familiarity with Perl matching is assumed, but we'll give an example to stimulate the interest in learning these capabilities of Perl. For instance, m/(\d+)\.?(\d*)(+)/| matches a valid number. Moreover, if the number had the form `xx.yy', then the Perl variable $1 will hold `xx' and $1 will hold `yy'. If the number was of the form `.zz', then $1 and $2 will be empty, and $3 will hold `zz'.

XSB-Perl interface provides access to all these special variables using the predicate get_match_result(). The input variables string and pattern are of XSB string data types. For example:

For instance,

:- try_match('First 11.22 ; next: 3.4', 'm/(+)?()/g').
yes.
finds the character which precedes by 's' in the string 'this is a test'. The first match is `is'.

Now, we can use get_match_result() to find the submatches. The first argument is a tag, which can be 1 through 20, denoting the Perl variables $1 - $20. In addition, the entire match can be found using the tag match, the part before that is hanging off the tag prematch and the part of the string to the right of the match is fetched with the tag postmatch. For instance, in the above, we shall have:

This function is used to fetch the pattern match results $1, $2, etc., $`, $', $& and $+, as follows:

:- get_match_result(1,X).
X=11
yes
:- get_match_result(2,X).
X=22
yes
:- get_match_result(3,X).
no
:- get_match_result(4,X).
no
:- get_match_result(prematch,X)
X=First  (including 1 trailing space)
yes
:- get_match_result(postmatch,X)
X= ; next 3.4
yes
:- get_match_result(match,X)
11.22
yes

As you noticed, if a tag fetches nothing (like in the case of Perl variables $3, $4, etc.), then the predicate fails.

The above is not the only possible match, of course. If we want more, we can call:

:- next_match.

This will match the second number in the string. Correspondingly, we shall have:

   :- get_match_result(1,X).
   X=3
   yes
   :- get_match_result(2,X).
   X=4
   yes
   :- get_match_result(3,X).
   no
   :- get_match_result(4,X).
   no
   :- get_match_result(prematch,X)
   X=First 11.22 ; next 
   yes
   :- get_match_result(postmatch,X)
   no
   :- get_match_result(match,X)
   3.4
   yes

The next call to next_match would fail, because there are no more matches in the given string. Note that next_match and get_match_result do not take a string and a pattern as argument--they work off of the previous try_match. If you want to change the string or the pattern, call try_match again (with different parameters).



Subsections
next up previous contents index
Next: Note: Up: perlmatch: Using Perl as Previous: perlmatch: Using Perl as   Contents   Index
Luis Fernando P. de Castro 2003-06-27