Great88
Great88

Reputation: 428

perl: search & replace by grouping

I am trying to update version in assemblyinfo file using perl command line (perl -i.bak -ape), I was thinking (obviously wrong) that s/\d+\.\d+\.(\d+).(\d+)/5.1/ would just replace the grouped, but actually it replaces entire version. here is what i need.

OLD=1.0.0.0
NEW=1.0.5.1

Upvotes: 2

Views: 94

Answers (2)

Kenosis
Kenosis

Reputation: 6204

Since you want to update a specific version, consider matching that version and use a positive lookbehind to keep the part you don't want to change:

s/(?<=1\.0\.)0\.0/5.1/

Hope this helps!

Upvotes: 2

Qtax
Qtax

Reputation: 33908

You could use \K to cut away the part you don't want to replace (keep everything till \K). Like so:

s/\d+\.\d+\.\K\d+\.\d+/5.1/

If your Perl version does not support \K (old), you could use a capturing group like:

s/(\d+\.\d+)\.\d+\.\d+/$1.5.1/

Also escaped the . for you, if you don't a . matches any character (except a new line without /s).

Upvotes: 3

Related Questions