Srinath
Srinath

Reputation: 1343

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..

Upvotes: 1

Views: 1396

Answers (2)

polygenelubricants
polygenelubricants

Reputation: 383696

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.

Upvotes: 5

Dónal
Dónal

Reputation: 187499

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'"', ''

Upvotes: 2

Related Questions