- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Place a piece on the board (ex: Qld1 – place the white queen on D1, Kde8 – place the black king on E8). Piece abbreviations are:
K = king
Q = queen
B = bishop
N = knight
R = rook
P = pawn
and l = light, d = dark.
Move a single piece on the board (ex: d8 h4 – moves the piece at D8 to the square at H4, c4 d6* - moves the piece at C4 to D6 and captures the piece at D6).
Move two pieces in a single turn (ex: e1 g1 h1 f1 – moves the king from E1 to G1 and moves the rook from H1 to F1. This is called a “king-side castle”).
我需要帮助编写一个正则表达式来获取列出的所有选项。我到目前为止:
([KQNBR]?([a-h]?[1-8]?x)?[a-h]([2-7]|[18](=[KQNBR])?)|0-0(-0)?)(\(ep\)|\+{1,2})?
或
([BKNPQR]?)([a-h]?)([0-9]?)([x=]?)([BKNPQR\*]|[a-h][1-8])( [+#]?)
在决定国际象棋棋盘将采用非常不同的自定义符号来处理移动之前。
问题是我需要帮助来创建一个正则表达式来验证这些国际象棋 Action 。
举个例子,这个程序不会实时操纵棋盘。但是,将从流中逐行读取文件,国际象棋游戏的控制台应用程序应读取每一行并为每个 Action 生成以下结果。
文件的前几行应该读取每个棋子的位置 Qld1 将白皇后放在 D1 上,kde8 将黑王放在 E8 上。
之后文件会读取每个 Action ,d8 h4会将d8位置的棋子移动到h4。
单个正则表达式将验证要读取的文本文件是否是基于其表达式的有效移动。如果无效,则跳过该移动并继续。
最佳答案
我创建了以下正则表达式,用于将复制的 chess24.com 游戏转换为 PGN-compatible游戏:
\s*(\d{1,3})\.?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?))\+?)(?:\s*\d+\.?\d+?m?s)?\.?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?))\+?)?(?:\s*\d+\.?\d+?m?s)?
替换字段为
\1. \2 \3\n
或者
$1. $2 $3\n
取决于您的正则表达式环境或正则表达式引擎。
Python 中的详细正则表达式:
chess_pattern = re.compile(r"""
\s* # Whitespace
(\d{1,3}) # Capture group 1: Move number between 1 and 999 will precede white side's move.
\.? # Literal period, in case move numbers followed by a period. The replace pattern will restore period, so it is not captured.
\s* # Whitespace
( # Capture group 2: This will collect the white side's move
(?: # Start non-capturing group A: Use vertical bar | between non-capturing groups to check for castling, piece moves/captures, pawn moves/captures/promotion
(?:O-O(?:-O)?) # Non-capturing subgroup A1: For castling kingside or queenside. Change the O to 0 to work for sites that 0-0 for castling notation
|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8]) # Non-capturing subgroup A2: For piece (non-pawn) moves and piece captures
|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?) # Non-capturing subgroup A3: Pawn moves, captures, and promotions
) # End non-capturing group A
\+? # Allow plus symbol for checks (attacks on king)
) # End capturing group 2: White side's move
(?:\s*\d+\.?\d+?m?s)? # Non-capturing group B: Skip over move-times; it is possible to retain move times if you make this a capturing group
\.? # Allow period in case a time ends in a decimal point
\s* # Whitespace
( # Capture group 3: This will collect the black side's move
(?: # Start non-capturing group C: Use vertical bar | between non-capturing groups to check for castling, piece moves/captures, pawn moves/captures/promotion
(?:O-O(?:-O)?) # Non-capturing subgroup C1: For castling kingside or queenside. Change the O to 0 to work for sites that 0-0 for castling notation
|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8]) # Non-capturing subgroup C2: For piece (non-pawn) moves and piece captures
|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?) # Non-capturing subgroup C3: Pawn moves, captures, and promotions
) # End non-capturing group C
\+? # Allow plus symbol for checks (attacks on king)
)? # End capturing group 3: Black side's move. Question mark allows final move to be white side's move without any subsequent black moves
(?:\s*\d+\.?\d+?m?s)? # Non-capturing group D: Skip over move-times; it is possible to retain move times if you make this a capturing group
""",re.VERBOSE)
# Paste the entire chess game inside the raw string below where there is currently ...
chess_game = """
...
"""
print( pattern.sub(r'\1. \2 \3 '+'\n',chess_game) ) # Will output PGN to console
# The following writes the PGN to a file `game.pgn` in the working directory
output_PGN = open('game.pgn','w+')
output_PGN.write(pattern.sub(r'\1. \2 \3 '+'\n',chess_game))
output_PGN.close()
请参阅此处的实际示例:regexr.com/58ngb
我还将上述内容实现为 Clipboard Fusion (C#) 宏:https://www.clipboardfusion.com/Macros/View/?ID=d220984d-faa4-4ba2-ab86-f16dceb42036
关于正则表达式挑战,自定义国际象棋符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638515/
给定一个字符串,例如 s="##$$$#",我如何找到索引之前的“#”符号数等于“”数的索引$"符号在索引之后? 示例:如果 s="##$$$#",则输出将为 2。 解释:在索引 2 之前我们有 2
在本教程中,您将借助示例了解 JavaScript 符号。 JavaScript 符号 JavaScript ES6 引入了一种新的原始数据类型,称为 Symbol(符号)。符号是不可变的(不能更改)
在“函数编程的工艺”一书中,符号 '>.>' 将函数连接在一起,与 '.' 的方向相反。但是当我使用 ghci 实现它时,它显示了超出范围的错误 '>.>'。为什么?它是不再使用的旧符号吗? 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要从向量中删除 \"。这是我的数据: data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1
我在 Nginx 配置中使用正则表达式来捕获文件 URL,但如果文件 URL 包含 # 符号,正则表达式模式将不会捕获它。 这里是nginx的配置部分。 location ~ ^/p/(?[\w\-=
如何使 & 符号在此图表的第一组条形/列下正确显示: http://jsfiddle.net/VxbrK/2/ 应该是“Apples & Oranges”而不是“Apples & Oranges”。
**在verilog中是什么意思? 我为测试台提供了以下逻辑 localparam NUM_INPUT_BITS = 1; localparam NUM_OUTPUT_BITS
我有一个使用正则表达式来验证电子邮件地址的方法。 public String searchFormail(String searchWord) { Pattern pattern = Patt
我想将一个字符串拆分为数字部分和文本/符号部分我当前的代码不包含负数或小数,并且表现得很奇怪,在输出的末尾添加了一个空列表元素 import re mystring = 'AD%5(6ag 0.33-
我有一些代码需要从数组中选择一个随机字符串,但它一直返回单个字母或数字。如何解决这个问题? var name = ["Yayek", "Vozarut", "Gezex",
我刚开始使用 Python,我在考虑应该使用哪种表示法。我读过 PEP 8关于 Python 符号的指南,我同意那里的大多数内容,除了函数名称(我更喜欢混合大小写风格)。 在 C++ 中,我使用匈牙利
在用 C# 编写代码时,我错误地在 if 语句中的变量前添加了一个符号(而不是感叹号)。 bool b = false; if (@b) { } 我很惊讶它编译成功,没有任何错误。 我想知道:上面的代
本文实例为大家分享了特殊字符替换电话号码中某一部分的方法,ios利用-号替换电话号码中间四位,供大家参考,具体内容如下 1、效果图 2、代码 rootviewcontroll
当我使用“x”和“z”作为符号时,这段代码没有问题: from sympy import * x, z = symbols('x z') y = -6*x**2 + 2*x*z**0.5 + 50*x
我需要从文本中删除标点符号: data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
我有几个数字是 numeric 类。下面的例子。 df = c(12974,12412,124124,124124,34543,4576547,32235) 现在我想在每个数字前添加 '$' 符号而不
我有一个 highcharts 图例,其中符号以不同的大小显示,因为它们在实际图表中的大小不同。不幸的是,当数据点的大小增加时,它们也会在图例中增加。无论数据点大小如何,我都希望图例符号保持相同的大小
我需要使用包含平均值+-SD的标题。到目前为止,我只能得到以下信息: "Mean +- SD or N (%)" [1] "Mean +- SD or N (%)" 如何直接使用“+-”符号?您知道一
使用 XSLT 和 XPath 1.0,我有一个要转义的字符串以用于 URL,例如: one word & another 因此,描述元素的 text() 应该进行 URL 转义。 我该怎么做
我是一名优秀的程序员,十分优秀!