Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

正規表現のCharacter Propertiesで通貨や平仮名、ラテン語までキャッチする

Character Propertiesをつかうと、
平仮名や片仮名、果ては通貨記号までキャッチする事が出来ます

/\p{hiragana}+/.match "ほげpiyoふが"
#=> #<MatchData "ほげ">
/\p{Sc}+/.match "¥120,000."
#=> #<MatchData "¥">

/\p{Sc}+/.match "$1,123."
#=> #<MatchData "$">

/\p{Sc}+/.match "130,000₫"
#=> #<MatchData "₫">

残念ながら、Unicode7から採用のロシアルーブルは未対応です

/\p{Sc}+/.match "100₽"
#=> nil
/[\p{latin}|\p{Blank}]+/.match "Si Vis Pacem, Para Bellum"
=> #<MatchData "Si Vis Pacem">

Class: Regexp (Ruby 2.0.0)