Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: regex replace a string

by OldChamp (Acolyte)
on Nov 04, 2017 at 17:45 UTC ( #1202756=note: print w/replies, xml ) Need Help??


in reply to Re: regex replace a string
in thread regex replace a string

The textfile contains text like this
[Event "?"] [Result "*"] [SetUp "1"] [FEN "4b3/pq2p1k1/1p1pP2R/1P1P2K1/2rp4/8/P5Q1/8 w - - 0 50"] [PlyCount "0"] * [Event "?"] [Round "39"] [Result "*"] [SetUp "1"] [FEN "5rk1/5p1p/3q2p1/1Rp1n3/P3P3/3Pb1pP/2Q3B1/3R3K b - - 0 1"]

I want to change [Event "?"] to [Event "Black to move"] if in the line [FEN .... ] I find a space, then b followed by another space

and to change it to [Event "White to move"] if in the line [FEN .... ] I find a space, then w followed by another space

I have tried to use lookahead, but as have not programmed anything in Perl for quite a while with no success.

Sorry for beeing unclear

2017-11-07 Athanasius added code tags

Replies are listed 'Best First'.
Re^3: regex replace a string (updated)
by AnomalousMonk (Bishop) on Nov 05, 2017 at 04:06 UTC

    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:
    • There can be any number, including zero, of lines between the Event and FEN lines, or they can be on the same line;
    • FEN is always on the same line as the b/w business (update: but see Update below);
    • Event-FEN-b/w sequences are never nested or interleaved;
    • Event-FEN-b/w all match case-sensitively.
    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:  <%-{-{-{-<

      Thank you for your help Anomalous Monk, I have applied the substitions you proposed and every thing works fine

      use strict; use warnings; my $text = do { local $/; <>; }; my %replacement = ( 'b' =>' Schwarz am Zug', 'w' => 'Weiß am Zug', ); ;; my $condition = qr{ \b (?: b | w) \b }xms; $text =~ s{ Event \s+ " \K [?] (?= " .*? FEN [^\n]* \s+ ($condition) \s+ -) } {$replacement{$1}}xmsg; print $text;
Re^3: regex replace a string
by davido (Cardinal) on Nov 04, 2017 at 19:09 UTC

    Yes, still unclear. Please read Writeup Formatting Tips to understand how to wrap sample data in either <code>...</code> or <pre>...</pre> tags. What you posted this time is rendered without the constraints of those sorts of tags, and thus some of the specifics we're looking to understand are lost. Why is all your text showing as links?


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-10-06 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (26 votes). Check out past polls.

    Notices?