Community
    • Login

    Please help me to replace text like the following condition

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 1.6k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Nguyen WanderozN Offline
      Nguyen Wanderoz
      last edited by

      I have a text file like the folowing one:
      I want to replace text according to those conditions:

      if in the [FEN] string, there is a w, (case sensitive) then replace the uper [Result"*“] to [Result"1-0”]

      if in the [FEN] string, there is a b (case-sensitive) then replace the uper [Result"**“] to [Result"0-1”]

      Please help me, is there any way to this in notepad++
      Thank you.

      • [Event “”]
        [Site “”]
        [Date “???.??.??”]
        [Round “?”]
        [White “”]
        [Black “”]
        [Result “*”]
        [ECO “?”]
        [WhiteElo “0”]
        [BlackElo “0”]
        [Annotator “”]
        [Source “”]
        [Remark “P(100,12) +WR 1 1 1 10 ; 2 11 5 4 48 ; 3 9 39 @ 35WT”]
        [SetUp “1”]
        [FEN “2rbr2k/1pq2ppp/p3bn2/4p1B1/P3P3/2N3Q1/1PP1B1PP/3R1R1K w - - 0 1”]
        1.Rxd8!
        Qxd8 ( 1…Rcxd8 2.Rxf6! ) 2.Qh4! Bc4 3.Rxf6
        Bxe2 4.Rh6! f6 5.Bxf6 gxf6 6.Rxh7+
        Kg8 7.Qh6 Rc7 8.Qg6+ Kf8 9.Rh8+ Ke7 10.Nd5+
        {1:0, Lukin - Timoshchenko, corr, 1979}

      • [Event “”]
        [Site “”]
        [Date “???.??.??”]
        [Round “?”]
        [White “”]
        [Black “”]
        [Result “*”]
        [ECO “?”]
        [WhiteElo “0”]
        [BlackElo “0”]
        [Annotator “”]
        [Source “”]
        [Remark “P +RU 1 1 1 10 ; 2 11 5 4 48 ; 3 9 39 @ 35WL”]
        [SetUp “1”]
        [FEN “3qr1k1/5ppp/4pn2/6B1/5RBQ/8/8/8 w - - 0 1”]
        1.Rxf6! gxf6 2.Bxf6 Qd5 3.Qh6

      • [Event “”]
        [Site “”]
        [Date “???.??.??”]
        [Round “?”]
        [White “”]
        [Black “”]
        [Result “*”]
        [ECO “?”]
        [WhiteElo “0”]
        [BlackElo “0”]
        [Annotator “”]
        [Source “”]
        [Remark “P +RD 1 1 2 1 ; 2 - ; 3 - @ 766BL”]
        [SetUp “1”]
        [FEN “8/8/8/3q1r2/8/4R1Q1/4B1PP/7K b - - 0 1”]
        1…Qd1+ 2.Bxd1 Rf1#

      1 Reply Last reply Reply Quote 0
      • guy038G Offline
        guy038
        last edited by guy038

        Hello @nguyen-wanderoz and All,

        No problem with a search using regular expressions ! And, as you precisely explain you needs, it was very easy to built the right S/R :-)) So :

        • Open the Replace dialog ( Ctrl + H )

        • Select the Regular expression search mode

        • Tick the Wrap option option

        SEARCH : (?s-i)(?<=\[Result\x20")\*(?=.*?\[FEN\x20.*?\x20((w)|b)(\x20-){2})

        REPLACE (?{2}1-0:0-1)

        • Click on the Replace All button, exclusively ( The step by step replacement, with the Replace button, does NOT work, because of the look-behind structure ! )

        Et voilà !


        Notes :

        • First, note that the [ symbol, ( as well as some others *, +, ?, |, … …) is a special regex character and, thus, must be escaped, with the \ symbol, to be taken literally

        • Note, also, that the \x20 syntax stands for a single space character. Of course, you may hit the space key, instead of writing \x20, in the Search what: zone

        • As usual, at beginning, the modifiers (?s-i) means that :

          • The dot ( . ) will match any single character, Standard or EOL one ( s )

          • The search is performed in a sensitive way ( -i )

        • This regex, essentially, look for the star character ( \* ), of the zone [Result "*"], but ONLY IF two conditions are TRUE :

          • It must be preceded, of course, with the string \[Result\x20", due to the fix-length look-behind (?<=\[Result\x20")

          • It must be followed , due to look-ahead structure (?=.*?\[FEN\x20.*?\x20((w)|b)(\x20-){2}), by :

            • The shortest range of any character till the string \[FEN\x20

            • Then, followed, again, with the shortest range of any character till the string \x20((w)|b)(\x20-){2}

        • Before the \x20-\x20- string, it searches for the lower-case letter w or b ( group 1 ) and note that when case w happens, it is stored as group 2 for later use in replacement

        • Now, due to the conditional replacement (?{2}1-0:0-1), the search match ( the * char ) is, then, changed into :

          • The string 1-0 if group 2 exists ( case w )

          • The string 0-1 if group 2 does not exist ( case b )

        Best Regards,

        guy038

        Nguyen WanderozN 1 Reply Last reply Reply Quote 2
        • Nguyen WanderozN Offline
          Nguyen Wanderoz @guy038
          last edited by

          @guy038 said:

          Replace All

          Hello @guy038 ,

          Thank you very much! It worked like magic!!! And your crystal cleared explaination make a dummy like me can get to understand. Surely, I need to start to learn about regular expressions.

          Best Regards,
          Wanderoz

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors