octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Backslashes in regular expression replacement patterns


From: Rik
Subject: Re: Backslashes in regular expression replacement patterns
Date: Wed, 17 Oct 2012 09:31:07 -0700

On 10/16/2012 02:37 PM, Ben Abbott wrote:
> On Oct 16, 2012, at 5:00 PM, Rik wrote:
>
>> On 10/16/2012 01:28 PM, Michael D. Godfrey wrote:
>>> On 10/16/2012 12:29 PM, Rik wrote:
>>>> regexprep ('a', '(\w)', "\\$1")
>>>>
>>>> Should it be '\a' or '$1'? 
>>>>
>>>> In the first case, one would get the same result from using a replacement
>>>> pattern of '\\$1' or "\\$1" which is odd because usually single quotes and
>>>> double quotes are not interchangeable.  On the other hand, Perl would
>>>> return '\a' for the regexprep above.
>>>>
>>> Matlab R2009b returns '\a'
>>>
>> Matlab has support for double-quoted strings now?  I thought they didn't 
>> support them, and this was only an Octave behavior question.
>>
>> --Rik
> No.  Only single-quoted strings
>
> regexprep ('a', '(\w)', '\\$1')
>
> ans =
>
> \a

10/17/12

All,

I have single-quoted strings working.  My real issue is double-quoted
strings which are specific to Octave.  With an expression like "\$1" the
interpreter itself does one round of escape sequence processing and changes
this to "$1".  So when I get the replacement string in regexprep I already
can't tell whether the user typed "\$1" or "$1".  I imagine making a
special case in the parser to not do double quote escape processing for a
single function might be difficult (any ideas jwe?).  If that is the case
then I think I will keep the current behavior of two rounds of expansion
and just document it.

--Rik


reply via email to

[Prev in Thread] Current Thread [Next in Thread]