Using perl lookbehind(or lookahead) to capture word

90 views Asked by At

I have a series of files each of which starts with the sentence

create tablespace tablespace-name data.dbf

I need to extract the tablespace-name and substitute data.dbf to be tablespace-name.dbf.

I'm new to perl, but looks like what I need is look behind (or look ahead) regex within perl.

How can this be accomplished?

1

There are 1 answers

6
ikegami On
if (defined( $_ = <> )) {
   s/^create \s+ tablespace \s+ (\S+) \s+ \K data (?= \.dbf \b )/$1/x;
   print;

   print while <>;
}

Technically, look-arounds and \K aren't needed here.

s/^ ( create \s+ tablespace \s+ ) (\S+) (\s+) data ( \.dbf \b )/$1$2$3$2$4/x

They just make things less noisy.