You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pig.apache.org by Danfeng Li <dl...@operasolutions.com> on 2012/09/07 23:06:47 UTC

REPLACE with backslash

Hi,

I'm little bit puzzled about REPLACE when there is backslash involved.

I want to replace all the "dir" in the string with "\\test\sub",

After a lot of try and error, I finally got it done, but I'm not sure why it is the case. Here's my code

A = load 'a.txt' as (name:chararray);
B = foreach A generate REPLACE(name,'dir','\\\\\\\\test\\\\sub');
dump B;

The thing I'm confused about is why do I need to use "\\\\" in order to generate a "\", can anyone explain the reason? I'm using pig 0.9.1.

Thanks.

Dan

Re: REPLACE with backslash

Posted by Віталій Тимчишин <ti...@gmail.com>.
I am sorry, this technique works in search patern param (second), not in
replacement string.


Субота, 8 вересня 2012 р. користувач Danfeng Li <dl...@operasolutions.com>
написав:
> You mean instead of
> REPLACE(name,'dir','\\\\\\\\test\\\\sub');
> Using
> REPLACE(name,'dir','[\\]test[\]sub');
> ?
>
> It seems not working for me.
>
> Thanks.
> Dan
>
> -----Original Message-----
> From: Віталій Тимчишин [mailto:tivv00@gmail.com]
> Sent: Saturday, September 08, 2012 12:39 AM
> To: user@pig.apache.org; billgraham@gmail.com
> Subject: Re: REPLACE with backslash
>
> I prefer [\\]. It does the same, but you don't need additional escaping.
>
> 2012/9/8 Bill Graham <bi...@gmail.com>
>
>> To get \ in a regex you need to escape it to \\, since the slash is
>> reserved in the regex. Then each slash needs to be escaped again to be
>> used in a java string. Hence \\\\. Painful, yes.
>>
>>
>> On Fri, Sep 7, 2012 at 2:06 PM, Danfeng Li <dl...@operasolutions.com>
wrote:
>>
>> > Hi,
>> >
>> > I'm little bit puzzled about REPLACE when there is backslash involved.
>> >
>> > I want to replace all the "dir" in the string with "\\test\sub",
>> >
>> > After a lot of try and error, I finally got it done, but I'm not
>> > sure why it is the case. Here's my code
>> >
>> > A = load 'a.txt' as (name:chararray); B = foreach A generate
>> > REPLACE(name,'dir','\\\\\\\\test\\\\sub');
>> > dump B;
>> >
>> > The thing I'm confused about is why do I need to use "\\\\" in order
>> > to generate a "\", can anyone explain the reason? I'm using pig 0.9.1.
>> >
>> > Thanks.
>> >
>> > Dan
>> >
>>
>>
>>
>> --
>> *Note that I'm no longer using my Yahoo! email address. Please email
>> me at billgraham@gmail.com going forward.*
>>
>
>
>
> --
> Best regards,
>  Vitalii Tymchyshyn
>

-- 
Best regards,
 Vitalii Tymchyshyn

RE: REPLACE with backslash

Posted by Danfeng Li <dl...@operasolutions.com>.
You mean instead of 
REPLACE(name,'dir','\\\\\\\\test\\\\sub');
Using 
REPLACE(name,'dir','[\\]test[\]sub');
?

It seems not working for me.

Thanks.
Dan

-----Original Message-----
From: Віталій Тимчишин [mailto:tivv00@gmail.com] 
Sent: Saturday, September 08, 2012 12:39 AM
To: user@pig.apache.org; billgraham@gmail.com
Subject: Re: REPLACE with backslash

I prefer [\\]. It does the same, but you don't need additional escaping.

2012/9/8 Bill Graham <bi...@gmail.com>

> To get \ in a regex you need to escape it to \\, since the slash is 
> reserved in the regex. Then each slash needs to be escaped again to be 
> used in a java string. Hence \\\\. Painful, yes.
>
>
> On Fri, Sep 7, 2012 at 2:06 PM, Danfeng Li <dl...@operasolutions.com> wrote:
>
> > Hi,
> >
> > I'm little bit puzzled about REPLACE when there is backslash involved.
> >
> > I want to replace all the "dir" in the string with "\\test\sub",
> >
> > After a lot of try and error, I finally got it done, but I'm not 
> > sure why it is the case. Here's my code
> >
> > A = load 'a.txt' as (name:chararray); B = foreach A generate 
> > REPLACE(name,'dir','\\\\\\\\test\\\\sub');
> > dump B;
> >
> > The thing I'm confused about is why do I need to use "\\\\" in order 
> > to generate a "\", can anyone explain the reason? I'm using pig 0.9.1.
> >
> > Thanks.
> >
> > Dan
> >
>
>
>
> --
> *Note that I'm no longer using my Yahoo! email address. Please email 
> me at billgraham@gmail.com going forward.*
>



--
Best regards,
 Vitalii Tymchyshyn

Re: REPLACE with backslash

Posted by Віталій Тимчишин <ti...@gmail.com>.
I prefer [\\]. It does the same, but you don't need additional escaping.

2012/9/8 Bill Graham <bi...@gmail.com>

> To get \ in a regex you need to escape it to \\, since the slash is
> reserved in the regex. Then each slash needs to be escaped again to be used
> in a java string. Hence \\\\. Painful, yes.
>
>
> On Fri, Sep 7, 2012 at 2:06 PM, Danfeng Li <dl...@operasolutions.com> wrote:
>
> > Hi,
> >
> > I'm little bit puzzled about REPLACE when there is backslash involved.
> >
> > I want to replace all the "dir" in the string with "\\test\sub",
> >
> > After a lot of try and error, I finally got it done, but I'm not sure why
> > it is the case. Here's my code
> >
> > A = load 'a.txt' as (name:chararray);
> > B = foreach A generate REPLACE(name,'dir','\\\\\\\\test\\\\sub');
> > dump B;
> >
> > The thing I'm confused about is why do I need to use "\\\\" in order to
> > generate a "\", can anyone explain the reason? I'm using pig 0.9.1.
> >
> > Thanks.
> >
> > Dan
> >
>
>
>
> --
> *Note that I'm no longer using my Yahoo! email address. Please email me at
> billgraham@gmail.com going forward.*
>



-- 
Best regards,
 Vitalii Tymchyshyn

Re: REPLACE with backslash

Posted by Bill Graham <bi...@gmail.com>.
To get \ in a regex you need to escape it to \\, since the slash is
reserved in the regex. Then each slash needs to be escaped again to be used
in a java string. Hence \\\\. Painful, yes.


On Fri, Sep 7, 2012 at 2:06 PM, Danfeng Li <dl...@operasolutions.com> wrote:

> Hi,
>
> I'm little bit puzzled about REPLACE when there is backslash involved.
>
> I want to replace all the "dir" in the string with "\\test\sub",
>
> After a lot of try and error, I finally got it done, but I'm not sure why
> it is the case. Here's my code
>
> A = load 'a.txt' as (name:chararray);
> B = foreach A generate REPLACE(name,'dir','\\\\\\\\test\\\\sub');
> dump B;
>
> The thing I'm confused about is why do I need to use "\\\\" in order to
> generate a "\", can anyone explain the reason? I'm using pig 0.9.1.
>
> Thanks.
>
> Dan
>



-- 
*Note that I'm no longer using my Yahoo! email address. Please email me at
billgraham@gmail.com going forward.*