in reply to Regex Parsing Chars in a Line

The . metacharacter matches spaces as well. From your input data it would appear that any hyphen surrounded by spaces is what you want to separate on? For example qr/\s\-\s/

You might also look at using Text::CSV_XS for the parsing. See https://metacpan.org/pod/Text::CSV_XS#sep for how to use multiple characters.

Replies are listed 'Best First'.
Re^2: Regex Parsing Chars in a Line
by kel (Sexton) on Nov 26, 2019 at 04:49 UTC
    By .+ I am targeting adjacent to anything, rather than spaces. And converting all but the first and last to underscores. I see a typo in my description that unnecessarily obfuscates....

      Maybe you could avoid the regex approach and use split and join?

      # untested my @parts = split /-/, $input; my $first = shift @parts; my $last = pop @parts; my $reassembled; # then $reassembled = $first . '-' . join ('_', @parts) . '-' . $last; # or something like this $reassembled = join '_', @parts; $reassembled = join '-', ($first, $reassembled, $last);

      The above assumes no CSV type quoting issues with embedded separators, for which a proper CSV parser like Text::CSV is needed.

      I see a typo in my description ...

      If you have not already done so (I don't see any Update), you might consider updating the errant node to clarify the mistake. Please see How do I change/delete my post?


      Give a man a fish:  <%-{-{-{-<