首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式,用于在pgn (国际象棋)中的“注释”周围添加卷括号

正则表达式,用于在pgn (国际象棋)中的“注释”周围添加卷括号
EN

Stack Overflow用户
提问于 2014-03-18 18:04:30
回答 1查看 309关注 0票数 1

编辑:要查看JS函数的工作版本,see the accepted answer in this followup thread

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

代码语言:javascript
复制
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 }“)。我的游戏来源是MS Word文档,游戏是以“国际象棋书”的形式格式化的,没有评论的花括号。

这是我的正则表达式,它匹配所有的“棋棋”块,只留下无法匹配的注释。基于测试,我认为我已经做了大约95%的正确。

代码语言:javascript
复制
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:http://regex101.com/r/dI8sQ8

[N.B.这部分

代码语言:javascript
复制
[NBRQK]?[a-h1-8]?x?[a-hO][1-8\-][!?+#=O-]{0,2}[NBRQ]*[!?+#]{0,2} 

发现国际象棋的移动,在较大的表达式中重复两次,因为有时评论只在白色移动(1. e4注释)之后出现,有时只在黑色移动之后出现(1.( e5注释),有时是在白色和黑色移动之后(1. e4 e5注释)]

到目前为止,除了注释之外,我可以匹配所有的moves...which块。剩下的就是用自己+a“}”和“{”之后“替换”这些匹配的移动块(可能跳过字符串的开头和结尾)。

我尝试了这两种替换功能:

代码语言:javascript
复制
str = str.replace(re,{\1});
str = str.replace(re,{$1});

我看到有人在这里做的事情:regular expression to add brackets before and after a repeated text

我看到了1美元那种东西的文件。我想我不太明白两者的区别,两者似乎都不起作用。

在链接的示例中,您还可以看到另一个问题,就是有时注释会出现在变体中(这些变量由"(“和")”分隔,如果一个变体以注释结尾,而另一个以注释开头,如“)( ",我们希望将其更改为”}“({”.但是我认为这很容易用第二个正则表达式来实现,在我们已经完成并添加了大部分{的正则表达式之后。

提前谢谢你的帮助。有些东西告诉我,我已经做了困难的部分,我只是不明白语法$1或\1

编辑:这里的是我正在使用的一个pgns的示例。

哈巴罗夫斯克是俄罗斯远东的首都。我16岁的对手是个很有前途的本地神童。现在他是一个很强的FM,FIDE评级为2437,并且活着.在美国也是!一个小小的世界。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终于有了准确的实战黑巩固了,但怀特仍然保持一定的压力,并有一些补偿的典当。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.Qf6+现在,在26...Kh6之后,一切都准备好准备一个决定性的打击。27 )。Qf6!Kh7 (没有27.Rxd6 28.cxd6 Rxd6?应付29。( Qh8# ) 28.g4!hxg4 29.h5 Rxd6 30.cxd6 Rxd6 31.hxg6+ Kg8 32.g7!到现在为止,这个棋子是至关重要的因素。任何其他动作,怀特都会输。32.。Qd8!对抗Qh6和Qh8的唯一防御手段33.Qh6 f5 34.Rd2!!这个想法是,白色的车不能再用支票拿走了。主教很容易被压倒性的Bxf5或Bc4所束缚。D文件上的黑针是个幻觉!事实上,被钉住的是布莱克的车,不能留下d文件。34..。Bd5 (最好的尝试-通过保护更多的e6典当来关闭d文件。没有帮助是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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-18 20:22:46

我认为您可以在一个正则表达式中匹配这些移动/注释。

只是建立一个新的字符串。下面,捕获grp 1是移动,grp2是评论。

编辑-简单更改。为了在JS中支持Dot,.被更改为[\S\s]

代码语言:javascript
复制
    # /((?:\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测试用例

代码语言:javascript
复制
$/ = 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

输出>>

代码语言:javascript
复制
 '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}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22487483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档