Re: regex.e find_all -- another problem
- Posted by jessedavis Mar 27, 2010
- 998 views
I agree that the documentation has problems; however, I think the following (actual terminal output) is what should happen. Notice that the find() routine returns only the first occurance and the find_all() returns a sequence that contains two sequence pairs, one for each occurance. Since the routine get_ovector_size() does not exist one needs the find_all routine to return something that does not require a lot of analysis to recover the found occurances, no matter how many ( none - all). This solution makes it easy to recover the occurances. Note that I have not looked into the question of other routines that depend on find_all() in regex.e....
Anyway here is the code and terminal output to demonstrate->
#!/home/jd/euphoria/bin/eui include std/pretty.e include std/regex.e as re sequence str_1 object b str_1 = "<property name=\"title\" translatable=\"yes\">My Window</property>" re:regex r1 = re:new("=\"[a-zA-Z ]") -- there are two of these re:regex r2 = re:new("george Vth") -- never find this one! puts(1,"\nfind results.............\n") b = re:find(r1,str_1) pretty_print(1,b,{0}) puts(1,"\nfind_all results.........\n") b = re:find_all(r1,str_1) pretty_print(1,b,{0}) puts(1,"\nfind_all null result.....\n") b = re:find_all(r2,str_1) pretty_print(1,b,{0}) puts(1,"\nend ....................\n")
Here is the terminal output------->
~/euphoria/jd_src>./reg_test.ex find results............. { {15,17} } find_all results......... { {15,17}, {36,38} } find_all null result..... -1 end ....................
Here is the modified find_all() routine from regex.e--->
public function find_all(regex re, sequence haystack, integer from=1, object options=DEFAULT) if sequence(options) then options = or_all(options) end if object result, results integer i results = -1 i = 0 while sequence(result) with entry do if i = 0 then results = result i += 1 else results = append(results, result[1]) end if from = max(result) + 1 if from > length(haystack) then exit end if entry result = find(re, haystack, from, options) end while return results end function
regards, jd