tags:

views:

42

answers:

2
+1  Q: 

regex in groovy

I need to remove "" from the both ends of the line

"/home/srinath/junk/backup"

and to display like this /home/srinath/junk/backup

How can we achieve this in groovy ?

thanks in advance, sri..

+2  A: 

You don't have to use a regex. If you always want to remove the first and last characters you can do this with

'"/home/srinath/junk/backup"'[1..-2]

Alternatively, to remove all double quotation marks use

'"/home/srinath/junk/backup"'.replaceAll'"', ''
Don
hi don, I used replaceAll and its working good . thanks
Srinath
+4  A: 

You want to replace ^"|"$ with the empty string. The ^ and $ are the beginning and end of string anchors, respectively. | is the alternation metacharacter.

References


Snippet

These were tested on lotrepls.appspot.com:

Groovy >>> println('"hello" "world"'.replaceAll('^"|"$',''));
hello" "world

Groovy >>> println('bleh'.replaceAll('^"|"$', ''));
bleh

Groovy >>> println(''.replaceAll('^"|"$', ''));
(blank)

As specified, replaceAll('^"|"$','') removes only doublequotes at the beginning and end of the string, if they're there. Internal doublequotes, if there are any, will be left untouched.

polygenelubricants
thanks, this will be more useful
Srinath