以下代碼是我開始測試在我正在使用的「德州撲克Em」風格遊戲中使用的內容。.match多字節字符問題?
我的問題是,當運行下面的代碼時,涉及「♥」的放入函數返回一個「\ u」的位置。我感到肯定的是這個多字節字符引起了第二個問題的原因,我用字符串數組中的d替換了它,它返回了我期待的內容。見下圖:
我的代碼:
#! /usr/bin/env ruby
# encoding: utf-8
table_cards = ["|2♥|", "|8♥|", "|6d|", "|6♣|", "|Q♠|"]
# Array of cards
player_1_face_1 = "8"
player_1_suit_1 = "♦"
# Player 1's face and suit of first card he has
player_1_face_2 = "6"
player_1_suit_2 = "♥"
# Player 1's face and suit of second card he has
test_str_1 = /(\D8\D{2})/.match(table_cards.to_s)
# EX: Searching for match between face values on (player 1's |8♦|) and the |8♥| on the table
test_str_2 = /(\D6\D{2})/.match(table_cards.to_s)
# EX: Searching for match between face values on (player 1's |6♥|) and the |6d| on the table
puts "#{test_str_1}"
puts "#{test_str_2}"
提出到屏幕:
|8\u
|6d|
- 我的目標是拿到首先將返回:| 8♥|
我沒有太多的尋找解決方案(甚至可能沒有),但更多的是「儘可能簡單」的解釋是什麼導致這個問題以及爲什麼。提前感謝任何有關此處發生的事情以及我如何解決目標的信息。
操作系統?什麼終端? – Anthony 2015-01-09 21:47:47
將Unicode字符打印到屏幕上涉及的不僅僅是Ruby腳本的正確編碼。它還要求您的控制檯使用相同的代碼集。如果這被設置爲別的東西,那麼這些事情就會發生。 Windows通常使用Win1252或ISO-8859-1,並且字符定義不匹配導致奇怪的輸出。 – 2015-01-09 21:49:03
這與'match'有關嗎?你嘗試過'puts「♥」'嗎? – Stefan 2015-01-09 22:26:01