[Gretl-users] Quotation marks (" ") and string manipulation

Allin Cottrell cottrell at wfu.edu
Wed May 20 09:40:05 EDT 2015


On Wed, 20 May 2015, Sven Schreiber wrote:

> Am 20.05.2015 um 10:35 schrieb Allin Cottrell:
>> On Tue, 19 May 2015, Sven Schreiber wrote:
>>
>
>> In hansl, the only way to create a string that contains nothing but a
>> double-quote is to use printf(). Hence the fact that Jack's approach works:
>>
>> string dq = sprintf("\"")
>> string s2 = regsub(s1, "\*", dq)
>>
>> In other words, there's no error in GLib's regexp code, and there isn't
>> really an error in gretl either -- it's just that at some point in the
>> past we decided not to mess with escapes in the straight definition of a
>> string literal, but only via printf.
>>
>
> Ok, but then I don't really understand the need for the co-existence of
> strsub() and regsub().

They are quite distinct, regardless of how the regular expressions are 
constructed on input. Lots of symbols have a special meaning in regsub 
but not in strsub (for example, '.').

> Or to put it differently, why not simply feed the string arguments 
> in regsub() always through sprintf(), such that effectively any call 
> regsub(a, b, c) would be handled as regsub(sprintf(a), sprintf(b), 
> sprintf(c)). Then the distinction between strsub() and regsub() 
> would be clearcut.

That's a cute idea (for strings b and c, anyway), and it works nicely 
for the few examples we've been looking at, but I'm afraid it doesn't 
work in general. For example, "%" has a special meaning in sprintf 
which will break any match/replace strings that are passed it.

<hansl>
string s1 = "20%x 30%y"
string s2 = regsub(s1, sprintf("%."), sprintf("percent"))
</hansl>

The above works fine without the sprintf calls, but with them it will 
choke on "%.".

Allin




More information about the Gretl-users mailing list