简体   繁体   English

使用正则表达式在文件中查找值

[英]Using Regex to find values in a file

I'm not really looking for syntax help, per say, but approach advice. 可以这么说,我并不是真正地在寻找语法帮助,而是寻求建议。 Here's what I need to do: 这是我需要做的:

I need to read some text in from a file containing moves (this is part of a chess game project) and then print out what move was performed. 我需要从包含动作的文件中读取一些文本(这是国际象棋游戏项目的一部分),然后打印出执行了哪些动作。 Here's the example text I'm using for the text file: 这是我用于文本文件的示例文本:

EDIT: to clarify, these each represent a move. 编辑:澄清一下,这些每个都代表一个动作。 ie RlA1 means white room moved to A1, NlB1 means white knight to B1, etc. 即RlA1表示将白色房间移至A1,NlB1表示将白色骑士移至B1,等等。

RlA1 NlB1 BlC1 QlD1 KlE1 BlF1 NlG1 RlH1
PlA2 PlB2 PlC2 PlD2 PlE2 PlF2 PlG2 PlH2
RdA8 NdB8 BdC8 QdD8 KdE8 BdF8 NdG8 RdH8
PdA7 PdB7 PdC7 PdD7 PdE7 PdF7 PdG7 PdH7
B1 C3* B2 B3 D8 H4

and here is what should be output for this example input: 这是此示例输入的输出:

A white rook is placed on A1 and a white knight is placed on B1 ... etc 
A white pawn ... etc 
A black rook ... etc 
A black pawn ... etc 
The piece on B1 moves to C3 and captures the piece there and the piece on B2 moves to B3 and the piece on D8 moves to H4

Obviously I'll use a toString() method override to print the moves back out, but I need to know how I should go about parsing the input with RegEx to determine what each move is. 显然,我将使用toString()方法重写来打印出这些动作,但是我需要知道如何使用RegEx解析输入以确定每个动作是什么。

Again, not really looking for syntax help so much as advice on how I should approach the problem (Pseudo) 再次,不是真正地寻求语法帮助,而是关于如何解决该问题的建议(伪)

I'm doing this in Java. 我正在用Java执行此操作。

First, you just want to split the string into the individual moves. 首先,您只想将字符串拆分为单个动作。 Do this by using your language's string split function. 通过使用您语言的字符串split功能来执行此操作。

After doing this, you can parse the individual moves. 完成此操作后,您可以解析单个动作。 Note though that chess moves can be quite complex in standard notation, so I'd suggest not trying to mash it all into one regexp. 请注意,尽管国际象棋的移动在标准符号中可能非常复杂,所以我建议不要尝试将它们全部融合到一个正则表达式中。 Rather, create helper function such as is_capture , is_castle , is_en_passant , get_moving_piece , and so on. 而是创建辅助函数,例如is_captureis_castleis_en_passantget_moving_piece等。

This depends on the language. 这取决于语言。 If its a compiled language i would do as tim suggested, split() and parse one letter at a time. 如果它是一种编译语言,我将按照tim的建议进行操作,split()并一次解析一个字母。

If its command land, then you can use awk for instance to grab each seperate one, space delimited, then parse one letter at a time again. 如果命令到达,则可以使用awk例如抓取每个用空格分隔的单独字母,然后再次解析一个字母。

Since you are writing a Chess Game project, I suggest formatting your moves file in an easier manner to parse. 由于您正在编写国际象棋游戏项目,因此建议您以一种更容易解析的方式来格式化您的动作文件。 Put all piece placements on one line. 将所有作品放置在一行上。 On the next line put the moves with a comma separating each move so something like: B1 C3*, B2 B3, D8 H4, etc. 在下一行中,用逗号隔开各个动作,例如:B1 C3 *,B2 B3,D8 H4等。

When you are reading in the file, read in the first line and split it on whitespace. 当您阅读文件时,请阅读第一行并将其在空白处分割。 Send each of the resulting array elements to a function that parses piece placement. 将每个结果数组元素发送到一个分析片段位置的函数。

Read in the next line and split it on comma. 阅读下一行,并以逗号分隔。 Send each of the resulting array elements to a function that parses the moves. 将每个结果数组元素发送到一个分析动作的函数。 Each element will contain the starting and ending place of the piece so you don't have to do as much crazy parsing. 每个元素都将包含片段的开始和结束位置,因此您不必进行太多疯狂的分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM