Groovy Links | 6 Aug 13:12
Favicon

[groovy-dev] escape character for $ in groovy


hi,

i have a method in groovy which gets a parameter as string. the string has a
value "This is to test $test_id. now i want to replace the $test_id with
"Missing". what is the exact escape character for $.

Note: the string which comes as a parameter has the $ symbol.

def static String replace(String footerText,  String replace)
   {
      footerText = footerText.replaceAll("\$",replace);

   }

in the above method the string footertext will hold "This is to test
$test_id". now i want to replace $test_id with "missing". but the above
replace statement doesn't work. 

can anyone tell the escape character for $ for the above scenario?

thanks

--

-- 
View this message in context: http://www.nabble.com/escape-character-for-%24-in-groovy-tp18849032p18849032.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

(Continue reading)

Jochen Theodorou | 7 Aug 12:23
Gravatar

Re: [groovy-dev] escape character for $ in groovy

Groovy Links schrieb:
> hi,
> 
> i have a method in groovy which gets a parameter as string. the string has a
> value "This is to test $test_id. now i want to replace the $test_id with
> "Missing". what is the exact escape character for $.
> 
> Note: the string which comes as a parameter has the $ symbol.
> 
> def static String replace(String footerText,  String replace)
>    {
>       footerText = footerText.replaceAll("\$",replace);
>       
>    }

your code is equal to this one here:

footerText.replaceAll('$',replace);

now $ has special meaning for a regexp, and it is easily overseen that 
the first argument to this method call is a string representing a 
regexp. $ means "end of line", therefor you have to escape it. So the 
right text should be

footerText.replaceAll('\\$',replace);

or

footerText.replaceAll("\\\$",replace);

(Continue reading)

Sergey Nebolsin | 7 Aug 14:14
Favicon
Gravatar

Re: [groovy-dev] escape character for $ in groovy

On 8/7/08, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
I would suggest:

footerText.replaceAll('\\$test_id',replace);

or

footerText.replaceAll(/\\$test_id/,replace);


 
I tried the last syntax you suggested, and it doesn't seem to work with groovy 1.5.6.

 
From groovyConsole:

 
groovy> println 'Hi, $aaa'.replaceAll(/\\$aaa/, 'Sergey')

 
Exception thrown: groovy.lang.MissingPropertyException: No such property: aaa for class: Script5

 
groovy.lang.MissingPropertyException: No such property: aaa for class: Script5
at Script5.run(Script5:1)

 

 

 
Using 'groovy test.groovy':

 
takedown:~ nebolsin$ groovy test.groovy 
Caught: groovy.lang.MissingPropertyException: No such property: aaa for class: test
at test.run(test.groovy:1)
at test.main(test.groovy)

 

 
Am I doing something wrong?

 
Cheers

 
--
Sergey Nebolsin
Principal Software Engineer, Prophotos.ru
Jochen Theodorou | 7 Aug 14:20
Gravatar

Re: [groovy-dev] escape character for $ in groovy

Sergey Nebolsin schrieb:
> On 8/7/08, *Jochen Theodorou* <blackdrag@... 
> <mailto:blackdrag@...>> wrote:
> 
>     I would suggest:
> 
>     footerText.replaceAll('\\$test_id',replace);
> 
>     or
> 
>     footerText.replaceAll(/\\$test_id/,replace);
> 
> 
>  
> I tried the last syntax you suggested, and it doesn't seem to work with 
> groovy 1.5.6. <http://1.5.6.>

then use the other version please... hmm.. now that I think about it... 
is it even possible to scape a $ in a regexp string?

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Sergey Nebolsin | 7 Aug 14:26
Favicon
Gravatar

Re: [groovy-dev] escape character for $ in groovy

On 8/7/08, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Sergey Nebolsin schrieb:
On 8/7/08, *Jochen Theodorou* <blackdrag-BA+cFGlbTmA@public.gmane.org <mailto:blackdrag-BA+cFGlbTmA@public.gmane.org>> wrote:

   I would suggest:

   footerText.replaceAll('\\$test_id',replace);

   or

   footerText.replaceAll(/\\$test_id/,replace);


 I tried the last syntax you suggested, and it doesn't seem to work with groovy 1.5.6. <http://1.5.6.>

then use the other version please... hmm.. now that I think about it... is it even possible to scape a $ in a regexp string?


 
I don't need this function currently, I just wanted to outline possible bug in Groovy :) If there's no way to escape $ in regexp string maybe worth raising a Jira?

 
Thanks

--
Sergey Nebolsin
Principal Software Engineer, Prophotos.ru
Peter Ledbrook | 7 Aug 14:45

Re: [groovy-dev] escape character for $ in groovy

> I don't need this function currently, I just wanted to outline possible bug
> in Groovy :) If there's no way to escape $ in regexp string maybe worth
> raising a Jira?

See the thread titled "lame regex question - escaping $" on groovy-user:

  http://www.nabble.com/lame-regex-question---escaping-%24-tt18781153.html#a18781153

Cheers,

Peter

--

-- 
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 8 Aug 10:29
Gravatar

Re: [groovy-dev] escape character for $ in groovy

Sergey Nebolsin schrieb:
[...]
>  
> I don't need this function currently, I just wanted to outline possible 
> bug in Groovy :) If there's no way to escape $ in regexp string maybe 
> worth raising a Jira?

yes, please do so... at last the $ at the end should be possible... 
hmm... I remember that Paul made many changes in that area.. when he is 
back I will ask him what to do about this. Until then let us use the 
JIRA entry to not to forget about it.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Sergey Nebolsin | 8 Aug 10:44
Favicon
Gravatar

Re: [groovy-dev] escape character for $ in groovy

Done, http://jira.codehaus.org/browse/GROOVY-2991


 
Cheers

On 8/8/08, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Sergey Nebolsin schrieb:
[...]
 I don't need this function currently, I just wanted to outline possible bug in Groovy :) If there's no way to escape $ in regexp string maybe worth raising a Jira?

yes, please do so... at last the $ at the end should be possible... hmm... I remember that Paul made many changes in that area.. when he is back I will ask him what to do about this. Until then let us use the JIRA entry to not to forget about it.


bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email





--
Sergey Nebolsin
Principal Software Engineer, Prophotos.ru

Gmane