Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regex Parsing Chars in a Line

by swl (Parson)
on Nov 24, 2019 at 20:53 UTC ( [id://11109158]=note: print w/replies, xml ) Need Help??


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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found