I don't have time to post a tested solution to your post sharpening the confusing specifications of the OPed problem. Whenever I post untested stuff, there's usually a huge bug right in the middle of it, but anyway...

The solution I posted here can fairly easily be adapted to your expanded, clarified specifications if you can load the entire file into memory at once, i.e., if the file is smaller than, say, a couple hundred MB (assuming your running on a bargain-basement laptop). I'm assuming you know how to "slurp" a file in this way and that it's been slurped into the  $s scalar. Then, change the  %replacement hash to

my %replacement = ( 'b' => 'Black to move', 'w' => 'White to move', );
and change the  s///g substitution to
$s =~ s{ Event \s+ " \K [?] (?= " .*? FEN [^\n]* \s+ ($string2) \s+ -) } {$replacement{$1}}xmsg;
This assumes that: After the  s///g substitution, write the modified  $s scalar out to a file.

If you know for sure that the mapping in the  %replacement hash will never change, you can get rid of the code building the  $string2 regex based on the hash and just define the regex as
    my $string2 = qr{ \b (?: b | w) \b }xms;
(although I hope you will give it a better name).

Again, this untested code still needs Perl version 5.10+ regex extensions (although it could fairly easily be adapted to an earlier version), and it's untested.

Update: On second thought, it seems to me that there's a possible problem with the  \s+ ($string2) \s+ sub-pattern of the
    Event \s+ " \K [?] (?= " .*? FEN [^\n]* \s+ ($string2) \s+ -)
regex that I've suggested for the  s/// match. The  \s class includes newline, so  \s+ could match a wild and crazy string like  "\t \n\t\n\n  \t" that is obviously not all one line as one of the assumptions stated above would have it. It may not make any practical difference, but I think I would rather use something like  [ \t]+ in place of  \s+ in the problematic sub-pattern, making it  [ \t]+ ($string2) [ \t]+ instead. Of course, the all-one-line assumption referred to may not actually be pertinent; in that case, no problem.


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


In reply to Re^3: regex replace a string (updated) by AnomalousMonk
in thread regex replace a string 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, 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.