Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

If

  • you want to substitute  'Weis' with  'Schwarz' (I will use only latin characters — simpler for me) only in those lines that immediately follow a line with a  'b' in it, and
  • (update: a  'b' never occurs in a line that has a  'Weis' you want to change, and)
  • you can slurp the entire file into memory at once, and
  • your Perl version is 5.10 or greater (for the  \K operator),
here's a way:
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $t = qq{ .....P1p1/6P1/7P/5PK1/8 w - - 4 34]\n} . qq{ {Weis am Zug} * .....\n} . qq{1n5P/1P4PK/1q6 b - - 2 42]\n} . qq{ {Weis am Zug} * .....\n} . qq{.......5K2/3R4 w - - 1 33]\n} . qq{ {Weis am Zug} *\n} ; print qq{[[$t]]}; ;; my $pre = qr{ \S+ \s }xms; my $post = qr{ \s [^\n]+ \n [^\{]+ \{ }xms; ;; $t =~ s{ ^ $pre b $post \K Weis \b} {Schwarz}xmsg; print qq{[[$t]]}; " [[ .....P1p1/6P1/7P/5PK1/8 w - - 4 34] {Weis am Zug} * ..... 1n5P/1P4PK/1q6 b - - 2 42] {Weis am Zug} * ..... .......5K2/3R4 w - - 1 33] {Weis am Zug} * ]] [[ .....P1p1/6P1/7P/5PK1/8 w - - 4 34] {Weis am Zug} * ..... 1n5P/1P4PK/1q6 b - - 2 42] {Schwarz am Zug} * ..... .......5K2/3R4 w - - 1 33] {Weis am Zug} * ]]
(Also note that I left out all the  " (double-quote) characters — they confuse my REPL.) If your Perl version is pre-5.10, let me know; there's a simple work-around.

Update: If your file is too large to fit entirely in memory and you must process line-by-line in a while-loop, your code might look something like this (untested):

use 5.010; ... my $pre_b = qr{ ... }xms; my $pre_weis = qr{ ... }xms; my $b_was_seen; while (my $line = <$input_filehandle>) { # check for trigger condition/substitution pattern sequence. # ASSUME: 'b' and 'Weis' cannot both occur in same line. if ($line =~ m{ \A $pre_b b }xms) { # flag 'b' was seen in this line. $b_was_seen = 1; } elsif ($b_was_seen) { # attempt substitution if 'b' seen in previous line. $line =~ s{ \A $pre_weis \K Weis }{Schwarz}xms; $b_was_seen = 0; } # write each (possibly altered) line to output file. print $output_filehandle $line; }
(Again, this assumes your Perl version is 5.10+.) The if-test could also be done, perhaps more concisely, with a  .. flip-flop operator (see Range Operators in scalar context in perlop), but verbosity may be a virtue here.


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


In reply to Re: Regex with condition by AnomalousMonk
in thread Regex with condition by OldChamp

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2019-08-21 16:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?