I like Eily's  next unless ...; approach++ best, but here's a variation on his or her  s///g solution using  \G that doesn't depend on hocus-pocusing pos:

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my @lines = ( 'A line with an_underscore.', 'A line with_two_underscores.', '~A line with an_underscore starting with a tilde.', '~A line with_two_underscores starting with a tilde.', ); ;; for my $line (@lines, @ARGV) { print qq{'$line'}; $line =~ s{ (?: \G (?! \A) | \A ~) .*? \K _ }{+}xmsg; print qq{'$line' \n}; } " "___" "~___" 'A line with an_underscore.' 'A line with an_underscore.' 'A line with_two_underscores.' 'A line with_two_underscores.' '~A line with an_underscore starting with a tilde.' '~A line with an+underscore starting with a tilde.' '~A line with_two_underscores starting with a tilde.' '~A line with+two+underscores starting with a tilde.' '___' '___' '~___' '~+++'

Update 1: FFR & FWIW, here's a version in the form of a Short, Self-Contained, Correct Example using a How to ask better questions using Test::More and sample data structure. Of course, the original question would have been submitted with plenty of test cases (and don't forget degenerate and simple cases!) and with the
    $input =~ s{ ... }{+}xmsg;
statement being raygun's current, unacceptable one, or maybe just a placeholder.

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my @test_set = ( 'degenerate and simple cases', [ '', '', 'empty line' ], [ ' ', ' ', 'single space' ], [ '~', '~', 'single tilde' ], [ '_', '_', 'single underscore' ], [ '~_', '~+', 'tilde, underscore' ], [ '_~', '_~', 'underscore, tilde' ], [ '__', '__', 'multiple underscores' ], [ '~__', '~++', 'tilde, multiple underscores' ], [ '__~', '__~', 'multiple underscores, tilde' ], 'more complicated cases', [ 'A line with an_underscore.', 'A line with an_underscore.', 'no leading tilde, text w/one underscore' ], [ 'A line with_two_underscores.', 'A line with_two_underscores.', 'no leading tilde, text w/two underscores' ], [ '~A line with an_underscore starting with a tilde.', '~A line with an+underscore starting with a tilde.', 'leading tilde, text w/one underscore' ], [ '~A line with_two_underscores starting with a tilde.', '~A line with+two+underscores starting with a tilde.', 'leading tilde, text w/two underscores' ], ); ;; VECTOR: for my $ar_vector (@test_set) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } ;; my ($input, $expected, $comment) = @$ar_vector; ;; $input =~ s{ (?: \G (?! \A) | \A ~) .*? \K _ }{+}xmsg; is $input, $expected, $comment; } ;; done_testing; ;; exit; " # degenerate and simple cases ok 1 - empty line ok 2 - single space ok 3 - single tilde ok 4 - single underscore ok 5 - tilde, underscore ok 6 - underscore, tilde ok 7 - multiple underscores ok 8 - tilde, multiple underscores ok 9 - multiple underscores, tilde # more complicated cases ok 10 - no leading tilde, text w/one underscore ok 11 - no leading tilde, text w/two underscores ok 12 - leading tilde, text w/one underscore ok 13 - leading tilde, text w/two underscores 1..13 ok 14 - no warnings 1..14
Update 2: Removed an extraneous  VECTOR: label that had crept into SSCCE code example. Code function unchanged.


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


In reply to Re: /g option not making s// find all matches (updated) by AnomalousMonk
in thread /g option not making s// find all matches by raygun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.