Please help me to replace text like the following condition
-
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#
-
-
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 theReplace
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 letterw
orb
( group1
) and note that when casew
happens, it is stored as group2
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 group2
exists ( casew
) -
The string
0-1
if group2
does not exist ( caseb
)
-
Best Regards,
guy038
-
-