in reply to Regex Parsing Chars in a Line

Did you mean "first and last hyphen" ? If so:

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11109157 use warnings; my $line = 'A A Milne - Winnie-The-Pooh and Silver-Bear vol5-12 - Xi P +ress - Peking (1998)'; print "$line\n"; $line =~ s/-\K.*(?=-)/ $& =~ tr'-'_'r /e; print "$line\n";

Outputs:

A A Milne - Winnie-The-Pooh and Silver-Bear vol5-12 - Xi Press - Pekin +g (1998) A A Milne - Winnie_The_Pooh and Silver_Bear vol5_12 _ Xi Press - Pekin +g (1998)

If this is not what you want, how about providing several test cases along with expected output.

Replies are listed 'Best First'.
Re^2: Regex Parsing Chars in a Line
by kel (Sexton) on Nov 26, 2019 at 05:16 UTC
    This seems to be what I am looking for: A simple (if complex) bit of code to simply keep firat and last hyphens in a book or multimedia title. I am unfamiliar with the K operator, and a quick google comes up with nothing. I do not normally use tr, but see here where it should be in my toolbox. Many thanks!