|
Post by donalds on Nov 14, 2011 10:18:12 GMT 1
How hard would it be to return more information if a match is found? Even the position and length of match would be helpful and how about a regular-expression replace method?
|
|
|
Post by Pjot on Nov 14, 2011 18:13:01 GMT 1
Hi,
BaCon uses the standard C implementation for regular expressions (defined in regex.h). The return value for the function 'regcomp' and 'regexec' is always either a 0 or a 1 (see 'man regcomp'):
Therefore a REPLACE using a regular expression cannot work because the location where the substring matches is not mentioned.
Regards Peter
|
|
|
Post by vovchik on Nov 15, 2011 19:47:35 GMT 1
Dear donalds, Peter is very correct about the REGEX implementation. Have a look at this page, which gives an explanation of the functions in regex.h: osix.net/modules/article/?id=349I agree with you in that it would be lovely to use REGEX for terse and convenient search and replace operations, but... With kind regards, vovchik
|
|
|
Post by donalds on Nov 15, 2011 20:30:17 GMT 1
Dear donalds, Peter is very correct about the REGEX implementation. Have a look at this page, which gives an explanation of the functions in regex.h: osix.net/modules/article/?id=349I agree with you in that it would be lovely to use REGEX for terse and convenient search and replace operations, but... With kind regards, vovchik looking at this link it says this comes from GNU regex which has the ability to return information about the match. see www.gnu.org/s/hello/manual/libc/Matching-POSIX-Regexps.html#Matching-POSIX-Regexpsso back to the original question - how hard would it be to expose this to the Bacon REGEX function? thanks Donald
|
|
|
Post by Pjot on Nov 15, 2011 21:28:43 GMT 1
Hi donalds, It seems my answer was too short Thing is, the REGEX function is implemented as a C macro which expands to a regular function (see lines 5087 to 5092 in bacon.bash). And a function only can return one value; this is the nature of BASIC functions. So, how would it be possible to return two values from one function? We could store the result in a reserved variable (ugly). Or instead of returning '0' or '1', return '0' or the number of the position where the match was found? This seems easy but might break previous programs using REGEX (like the BaCon version and GUI version). Regards Peter
|
|
|
Post by donalds on Nov 16, 2011 8:33:14 GMT 1
Thanks Peter
That's clearer. I could perhaps try modifying it for my own use and see what I come up with.
Donald
|
|
|
Post by Pjot on Sept 1, 2012 19:25:11 GMT 1
So as a test for build 27, I have changed REGEX in a way that it returns the position of the first match, counting from the left of the target string. A a closer look, the BaCon and BaConGUI implementations only check if REGEX returns a '0' or a non-'0'. So these should work fine. Hope this does not break anything else for anybody. Regards Peter
|
|