[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Backslashes in regular expression replacement patterns
From: |
Juan Pablo Carbajal |
Subject: |
Re: Backslashes in regular expression replacement patterns |
Date: |
Wed, 17 Oct 2012 19:42:40 +0200 |
On Wed, Oct 17, 2012 at 6:31 PM, Rik <address@hidden> wrote:
> 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
Hi Rik,
"\$1" is an unrecognized escape sequence. You get a warning.
"\\$1" produces the same as '\$1' which looks correct to me.
- Backslashes in regular expression replacement patterns, Rik, 2012/10/16
- Re: Backslashes in regular expression replacement patterns, Michael D. Godfrey, 2012/10/16
- Re: Backslashes in regular expression replacement patterns, Rik, 2012/10/16
- Re: Backslashes in regular expression replacement patterns, Ben Abbott, 2012/10/16
- Re: Backslashes in regular expression replacement patterns, Rik, 2012/10/17
- Re: Backslashes in regular expression replacement patterns, John W. Eaton, 2012/10/17
- Re: Backslashes in regular expression replacement patterns,
Juan Pablo Carbajal <=
- Re: Backslashes in regular expression replacement patterns, John W. Eaton, 2012/10/17
- Re: Backslashes in regular expression replacement patterns, Rik, 2012/10/17
- Re: Backslashes in regular expression replacement patterns, John W. Eaton, 2012/10/18