7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

将棋の指し手の正規表現の例

Last updated at Posted at 2017-11-14

表記チェックというよりは、分解するための正規表現です。

@regexp = /
  (?<to>[1-91-9一二三四五六七八九]{2})?
  (?<same>同)?
  \p{blank}*
  (?<piece>成[銀桂香]|[王玉金銀全桂圭香杏角馬飛龍竜歩と])
  (?<suffix>[左右直]?[寄引上行]?)
  (?<motion>不?成|打|合|生)?
  (\((?<from>\d{2})\))? # KIFフォーマットの移動元用
/x

def _(v) v.match(@regexp).named_captures end

_ "7六歩"         # => {"to"=>"7六", "same"=>nil, "piece"=>"歩", "suffix"=>"", "motion"=>nil, "from"=>nil}
_ "22角成(88)"     # => {"to"=>"22", "same"=>nil, "piece"=>"角", "suffix"=>"", "motion"=>"成", "from"=>"88"}
_ "同 飛"         # => {"to"=>nil, "same"=>"同", "piece"=>"飛", "suffix"=>"", "motion"=>nil, "from"=>nil}
_ "5八成銀右"     # => {"to"=>"5八", "same"=>nil, "piece"=>"成銀", "suffix"=>"右", "motion"=>nil, "from"=>nil}
_ "58成香右上"   # => {"to"=>"58", "same"=>nil, "piece"=>"成香", "suffix"=>"右上", "motion"=>nil, "from"=>nil}
_ "4一桂不成"     # => {"to"=>"4一", "same"=>nil, "piece"=>"桂", "suffix"=>"", "motion"=>"不成", "from"=>nil}
_ "5五玉打(59)"   # => {"to"=>"5五", "same"=>nil, "piece"=>"玉", "suffix"=>"", "motion"=>"打", "from"=>"59"}
_ "5五同王打(51)" # => {"to"=>"5五", "same"=>"同", "piece"=>"王", "suffix"=>"", "motion"=>"打", "from"=>"51"}

参照

日本将棋連盟 棋譜の表記方法
https://www.shogi.or.jp/faq/kihuhyouki.html
※とても詳しいが「行」の意味については触れられてない

品川将棋倶楽部 将棋のルール「棋譜について」
https://ameblo.jp/written-by-m/entry-10365417107.html
※昔の棋譜にある「行」の意味についての言及がある

棋譜 - Wikipedia
https://ja.wikipedia.org/wiki/%E6%A3%8B%E8%AD%9C
※「生」とは「不成」のこと

7
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?