使用正则表达式模式提取键值对

时间:2016-09-29 16:57:08

标签: c# regex datetime

我正在尝试从像这样的字符串中获取键值对

public class GameState implements Cloneable{ private Node[][] board; // Game board private int scorePlayer, scoreAI; // Player scores ( will start at 0 ) private ArrayList<Node> validNodes; // List holding all nodes possible to add pieces to /** * Creates the game state */ public GameState(){ // create size x size board this.board = new Node[Setting.BOARD_SIZE][Setting.BOARD_SIZE]; validNodes = new ArrayList<>(); scorePlayer = 0; scoreAI = 0; protected GameState clone() { return new GameState(this); }------------------------ CLONE METHOD---------------- public int search(GameState board, Player player, int alpha, int beta, int depth, ScoreEval function) { int record = Integer.MIN_VALUE; Node maxMove = null; int result; GameState subBoard = board.clone(); if (depth <= 0 || board.getValidMoves().size()==0) { record = function.evaluate(board, player); } else { ArrayList<Node> possibleMoves = board.getValidMoves(); if (!possibleMoves.isEmpty()) { for (int i =0; i<possibleMoves.size();i++) { Node nod = possibleMoves.get(i); subBoard = board.clone(); subBoard.setPiece(nod.x,nod.y, player.type); if(player.type==Setting.TILE_AI){ result = -search(subBoard, aiAss1.Controller.pHum, alpha, beta, depth - 1, function); } else{ result = -search(subBoard, aiAss1.Controller.pAI, alpha, beta, depth - 1, function); } if (result > record) { record = result; maxMove = nod; } } } else { record = -search(subBoard, player, alpha, beta, depth - 1, function); } } bestMove = maxMove; return record; }

想要收到像这样的派对

TestKeyAAA: Hello World TestKeyBBB: 987654321 TestKeyCCC: A long sentence with a date time in it 2016-09-29T20:15:11 some more text TestKeyDDD: 123456789

我正在使用的模式是

TestKeyAAA = „Hello World“ TestKeyBBB = „987654321“ TestKeyCCC = „A long sentence with a date time in it 2016-09-29T20:15:11 some more text“ TestKeyDDD = „123456789“

除了关键TestKeyCCC的值之外,它的效果很好。问题是日期中的“:”。

密钥只包含(.+?):(.+?)(?=(?:[^\s]+:)|(?:$))

我用以下模式尝试了它

[A-Z],[a-z],. and /

但这不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:1)

这似乎适用于您的测试场景。

(?<key>[A-Za-z\.,/]+?):\s(?<value>.+?)(?=\s[A-Za-z\.,/]+?:|$)

答案 1 :(得分:1)

我认为在匹配&#34;时需要排除数字和空格:&#34;

([A-Za-z\.]+?):(.+?)(?=(?:[^\s0-9]+:)|(?:$))