Product SiteDocumentation Site

5.4.16.5. pos


>>-pos-(-haystack-)----------------------------------------><

This method tries to locate a string defined by the regular expression on the "new" invocation or on the "parse" invocation in the given haystack string. It returns 0 on an unsuccessful match or the starting position on a successful match. The end position of the match can be retrieved with the position method (Section 5.4.16.6, “position”).

Example 5.199. Class REGULAREXPRESSION - haystack method


str = "It is the year 2002!"
myRE = .RegularExpression~new("[1-9][0-9]*")
begin = myRE~pos(str)
if begin > 0 then do
  year = str~substr(begin, myRE~position - begin + 1)
  say "Found the number" year "in this sentence."
end

::requires "rxregexp.cls"


Output:

Found the number 2002 in this sentence.