我需要将一个字符串从服务器传递给客户端。该字符串以“P1”开头,后跟一张扑克牌的名称。我想在客户端使用正则表达式来检查它是否以“P1”开头,如果是,则从字符串的其余部分中提取卡片信息并显示该卡片。但是,我似乎无法让正则表达式识别传入的字符串。
服务器端:
for(int i = 0; i < players.length; i++) {
imagePath1 = players[i].getCard(0).getImagePath();
imagePath2 = players[i].getCard(0).getImagePath();
output.format("%s\n", "Pocket Cards");
output.flush();
output.format("%s\n", "P1, " + imagePath1);
output.flush();
}
客户端:
public void run() {
playerNumber = input.nextLine();
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
lblIdField.setText("You are Player " + playerNumber + ".");
}
}
);
myTurn = (playerNumber.equals("Player 1"));
while(true) {
if(input.hasNextLine()) {
processMessage(input.nextLine());
}
}
}
public void processMessage(String message) {
if(message.equals("Pocket Cards")) {
//displayPocketCards("resources/10C.png");
}
else if(message.matches("P1, [.]*")) {
String[] split = message.split(", ");
displayPocketCards("resources/10C.png");
}
}