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.
Update 2: Removed an extraneous VECTOR: label that had crept into SSCCE code example. Code function unchanged.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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |