Javascript 正则表达式,在“";评论“;在pgn(国际象棋)中

Javascript 正则表达式,在“";评论“;在pgn(国际象棋)中,javascript,regex,replace,Javascript,Regex,Replace,编辑:要查看JS函数的工作版本 编辑:我使用的最后一个正则表达式是: var pattern = /((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]

编辑:要查看JS函数的工作版本

编辑:我使用的最后一个正则表达式是:

var pattern = /((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})[^\)\(])+)|((?:\)\s\())/g;

我正在把一堆国际象棋游戏转录成官方的PGN格式。通常,PGN格式的“注释”放在花括号内(例如“{blah blah blah})。我的游戏源是MS Word文档,其中游戏的格式为“棋谱”样式,注释周围没有花括号

这是我的正则表达式,它匹配所有的“棋步”,只留下不匹配的注释。根据测试,我想我已经做了大约95%的正确率

var re = /(/(\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8\-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2}(\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8\-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2})?\s?[\(\)]?\s?[\(\)]?\s?)+/gm;
您可以在这里看到我的异常长的正则表达式以及示例pgn:

[N.B.这块

[NBRQK]?[a-h1-8]?x?[a-hO][1-8\-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2} 
它会在较大的表达式中重复两次,因为有时注释仅出现在白色移动之后(1.e4注释),有时出现在黑色移动之后(1…e5注释),有时出现在白色和黑色移动之后(1.e4 e5注释)]

到目前为止,我可以匹配所有的移动块…除了注释之外,其他都是。剩下的就是将这些匹配的移动块“替换”为前面加上“}”和后面加上“{”(可能跳过字符串的开头和结尾)

我尝试了以下两种替换功能:

str = str.replace(re,{\1});
str = str.replace(re,{$1});
我看到有人在这里做的\1件事:

我看到了关于1美元的文件,我想我不太明白其中的区别,而且两者似乎都不起作用

在链接示例中可以看到的另一个问题是,有时注释出现在变体中(由“(”and“)分隔),如果一个变体以注释结尾,另一个变体以注释开头,看起来像“)(”,我们希望将其更改为“}”({” …但我认为,在我们用这个正则表达式完成并添加了大部分{之后,用第二个正则表达式很容易做到这一点

提前感谢您的帮助。有些事情告诉我,我已经完成了最难的部分,只是不理解语法$1或\1

编辑:这是我正在使用的一个PGN的示例


哈巴罗夫斯克是俄罗斯远东地区的首府。我16岁的对手是一个很有前途的当地神童。现在他是一个非常强大的FM,拥有2437的诚信度,并且生活在美国!一个小世界。1.e4 C52.Nf3 e6 3.c3 Nf6 4.e5 Nd5.d4 CXD6.Nc3 Nc6 8.Bd3!?Nxc3 9.bxc3 dxe5 10.dxe5 Qa5 11.O-O Be7 12.QNX Nxe5 13e5 Qxe5 14.Bb5+Kf8 15.Ba3 Qc7 16.Rad1 g6 17.c4!Bxa3 18.Qxa3+Kg7 19.Rd6 Rd8 20.c5 Bd7 21.Bc4 Bc6 22.Rfd1 Rd7 23.Qg3 Rad8最终凭借准确、稳定的打法,黑色得到了巩固,但白色仍然保持了一些压力,并对棋子进行了一些补偿。24.h4在这样的位置上进行一次典型的行进,没有其他更好的了。24…h5?!(24…h6会是一个更谨慎的反应。)(但最好的防御是24…Rd6!25.cd6 Qa5)25.Qe5+Kh7 26.Bd3非常自然26…Kh6?(缺失26…Ba4!27.Qxh5+Kg7 28.Qe5+Kg8!现在黑人有很多自己的威胁。白人必须在29.h5!Bxd1 30.h6 f6 31.Qxf6 Bh5 32.Qxe6+Kh7 33.Bxg6+Bxg6 34.Qxg6+Kh8 35.现在,26…Kh6之后,一切都准备好了准备决定性的打击。)27.Qf6!Kh7(由于29.Qh8#,没有27…Rxd6 28.cxd6 Rxd6?)28.g4!hxg4 29.h5 Rxd6 30.cxd6 Rxd6 31.hxg6+Kg8 32.g7!这个棋子在现在结束之前是至关重要的因素。任何其他的动作,怀特都会输。32…Qd8!对抗Qh6和Qh8的唯一防御手段是对弈或对弈。33.Qh6 f5 34.Rd2!!这个想法是白色的车再也不能用支票接受了。主教将很容易地与c分开冲刺Bxf5或Bc4。d文件上的黑色pin是一种错觉!事实上,是黑色的车被钉住了,不能离开d文件。34…Bd5(最好的尝试是关闭d文件,保护更多e6典当。没有帮助的是34…Rd7 35.Bf5 ef5 36.Qh8 Kf7 37.Rd7)(但也许最好的实际机会是34…g3!?而现在35。Bxf5没有赢,因为35…gxf2+36。Kh2 f1=N+!37.Kh3 Bg2+!38.Rxg2 Rd3+!39.Bxd3 Qxd3+,具有惊人的永久性40.Kh4 Qe4+41.Rg4 Qh1+42.Kg5 Qd5+43.Kf6 Qd8+44.Kg6 Qd3+)(但是在34…g3!之后,怀特用另一种翼战术赢了:35.Bc4!Bd5 36.Bxd5 exd5 37.Qh8+Kf7 38.Rc2 gxf2+39.Kf1!并且对Rc8没有防御。现在在35…Bd5之后,一切看起来都得到了很好的保护。)35.Qh8 Kf7 36.Bb5!主教仍然在突破。即将到来的Be8是一个杀手。36…Qg8 37.Be8+!Qxe8 38.Qe8+Kxe8 39.g8=Q+Kd7 40.Qg7+这是怀特的第40次行动,这意味着时间控制对我来说已经结束了。我时间不够了。一个女王只有一块和三个棋子是不够的。布莱克辞职了。1-0我想你可以辞职h一个正则表达式中的移动/注释。
建立一个新字符串。下面,Capture grp 1是移动,grp2是注释

编辑-简单更改。为了支持JS中的点全部,将
更改为
[\S\S]

    # /((?:\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-])[\S\s])+)/

    (                                  # (1 start), Moves
         (?:
              \s? 
              [()]?  \s? [()]? 
              \s? 
              [0-9]{1,3} \.{1,3} 
              \s 
              [NBRQK]? [a-h1-8]? x? [a-hO] [1-8-] [!?+#=O-]{0,2} [NBRQ]* [!?+#]{0,2} 
              (?:
                   \s 
                   [NBRQK]? [a-h1-8]? x? [a-hO] [1-8-] [!?+#=O-]{0,2} [NBRQ]* [!?+#]{0,2} 
              )?
              \s? 
              [()]? \s? [()]? 
              \s? 
         )+
    )                                  # (1 end)
 |  
    (                                  # (2 start), Comments
         (?:
              (?!
                   \s? 
                   [()]?  \s? [()]? 
                   \s? 
                   [0-9]{1,3} \.{1,3} 
                   \s 
                   [NBRQK]? [a-h1-8]? x? [a-hO] [1-8-] 
              )
              [\S\s]                   # Changed from `.` JS doesn't support Dot-All
         )+
    )                                  # (2 end)
Perl测试用例

$/ = undef;

$str = <DATA>;

$newstr = '';

while ( $str =~ /((?:\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-])[\S\s])+)/g )
{
    if (defined $1) {
       $newstr .= $1;
    }
    else {
       $newstr .= '{' . $2 . '}';
    }
    print "'$&'\n";
}

print "---------------\n";
print $newstr;


__DATA__
Khabarovsk is the capital of Far East of Russia. My 16-year-old opponent was a promising local prodigy. Now he is a very strong FM with a FIDE rating of 2437 and lives... in the USA, too! A small world. 1. e4 c5 2. Nf3 e6 3. c3 Nf6 4. e5 Nd5 5. d4 cxd4 6. cxd4 d6 7. Nc3 Nc6 8. Bd3!? Nxc3 9. bxc3 dxe5 10. dxe5 Qa5 11. O-O Be7 12. Qb3 Nxe5 13. Nxe5 Qxe5 14. Bb5+ Kf8 15. Ba3 Qc7 16. Rad1 g6 17. c4! Bxa3 18. Qxa3+ Kg7 19. Rd6 Rd8 20. c5 Bd7 21. Bc4 Bc6 22. Rfd1 Rd7 23. Qg3 Rad8 Finally with accurate, solid play Black has consolidated yet White still keeps some pressure and has some compensation for the pawn. 24. h4 A typical march in such positions, simply nothing else to do better. 24... h5?! ( 24... h6 would be a more careful response. ) ( But the best defense was 24... Rd6! 25. cd6 Qa5 ) 25. Qe5+ Kh7 26. Bd3 Very natural 26... Kh6? ( Missing 26... Ba4! 27. Qxh5+ Kg7 28. Qe5+ Kg8! and now Black has many own threats. White would have to force a perpetual after 29. h5! Bxd1 30. h6 f6 31. Qxf6 Bh5 32. Qxe6+ Kh7 33. Bxg6+ Bxg6 34. Qxg6+ Kh8 35. Qf6+ Now, after 26...Kh6 everything is ready for preparing a decisive blow. ) 27. Qf6! Kh7 ( There is no 27... Rxd6 28. cxd6 Rxd6? due to 29. Qh8# ) 28

请添加一个标记以指示您使用的是哪种编程语言。不同语言之间的Regex不同,您也会询问代码的其他方面。发布一个匹配的国际象棋数据示例。我使用的是regex101拒绝使用的旧浏览器。添加了一个标记。我使用的是Javascript。很抱歉。我不知道任何Perl,但是我能猜出语法,(明天会查出来)…无论如何,我想我会按照你做的做。感谢你的代码,以及构建新字符串的想法。看起来更聪明:-)一旦我开始工作,威尔+1@fr0ggE-与Perl无关。这是关于正则表达式的,它在JS中也会这样做。它只是匹配移动或注释。您还可以使用回调进行全局替换。如果移动匹配(即长度\1>0),则替换为\1(移动)。如果注释匹配(即。
 'Khabarovsk is the capital of Far East of Russia. My 16-year-old opponent was a promising local prodigy. Now he is a very strong FM with a FIDE rating of 2437 and lives... in the USA, too! A small world.'
 ' 1. e4 c5 2. Nf3 e6 3. c3 Nf6 4. e5 Nd5 5. d4 cxd4 6. cxd4 d6 7. Nc3 Nc6 8. Bd3!? Nxc3 9. bxc3 dxe5 10. dxe5 Qa5 11. O-O Be7 12. Qb3 Nxe5 13. Nxe5 Qxe5 14. Bb5+ Kf8 15. Ba3 Qc7 16. Rad1 g6 17. c4! Bxa3 18. Qxa3+ Kg7 19. Rd6 Rd8 20. c5 Bd7 21. Bc4 Bc6 22. Rfd1 Rd7 23. Qg3 Rad8 '
 'Finally with accurate, solid play Black has consolidated yet White still keeps some pressure and has some compensation for the pawn.'
 ' 24. h4 '
 'A typical march in such positions, simply nothing else to do better.'
 ' 24... h5?! ( 24... h6 '
 'would be a more careful response. ) ( But the best defense was'
 ' 24... Rd6! 25. cd6 Qa5 ) 25. Qe5+ Kh7 26. Bd3 '
 'Very natural'
 ' 26... Kh6? ( '
 'Missing'
 ' 26... Ba4! 27. Qxh5+ Kg7 28. Qe5+ Kg8! '
 'and now Black has many own threats. White would have to force a perpetual after'
 ' 29. h5! Bxd1 30. h6 f6 31. Qxf6 Bh5 32. Qxe6+ Kh7 33. Bxg6+ Bxg6 34. Qxg6+ Kh8 35. Qf6+ '
 'Now, after 26...Kh6 everything is ready for preparing a decisive blow.'
 ' ) 27. Qf6! Kh7 ( '
 'There is no'
 ' 27... Rxd6 28. cxd6 Rxd6? '
 'due to'
 ' 29. Qh8# ) '
 '28'
 ---------------
 {Khabarovsk is the capital of Far East of Russia. My 16-year-old opponent was a promising local prodigy. Now he is a very strong FM with a FIDE rating of 2437 and lives... in the USA, too! A small world.} 1. e4 c5 2. Nf3 e6 3. c3 Nf6 4. e5 Nd5 5. d4 cxd4 6. cxd4 d6 7. Nc3 Nc6 8. Bd3!? Nxc3 9. bxc3 dxe5 10. dxe5 Qa5 11. O-O Be7 12. Qb3 Nxe5 13. Nxe5 Qxe5 14. Bb5+ Kf8 15. Ba3 Qc7 16. Rad1 g6 17. c4! Bxa3 18. Qxa3+ Kg7 19. Rd6 Rd8 20. c5 Bd7 21. Bc4 Bc6 22. Rfd1 Rd7 23. Qg3 Rad8 {Finally with accurate, solid play Black has consolidated yet White still keeps some pressure and has some compensation for the pawn.} 24. h4 {A typical march in such positions, simply nothing else to do better.} 24... h5?! ( 24... h6 {would be a more careful response. ) ( But the best defense was} 24... Rd6! 25. cd6 Qa5 ) 25. Qe5+ Kh7 26. Bd3 {Very natural} 26... Kh6? ( {Missing} 26... Ba4! 27. Qxh5+ Kg7 28. Qe5+ Kg8! {and now Black has many own threats. White would have to force a perpetual after} 29. h5! Bxd1 30. h6 f6 31. Qxf6 Bh5 32. Qxe6+ Kh7 33. Bxg6+ Bxg6 34. Qxg6+ Kh8 35. Qf6+ {Now, after 26...Kh6 everything is ready for preparing a decisive blow.} ) 27. Qf6! Kh7 ( {There is no} 27... Rxd6 28. cxd6 Rxd6? {due to} 29. Qh8# ) {28}