{"id":7029,"date":"2019-11-17T10:50:59","date_gmt":"2019-11-17T01:50:59","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7029"},"modified":"2023-05-27T22:34:59","modified_gmt":"2023-05-27T13:34:59","slug":"%e3%80%90ruby%e3%80%91%e6%ad%a3%e8%a6%8f%e8%a1%a8%e7%8f%be%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6%e3%81%ae%e7%9f%a5%e8%ad%98","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7029","title":{"rendered":"\u3010Ruby\u3011\u6b63\u898f\u8868\u73fe\u306b\u3064\u3044\u3066\u306e\u77e5\u8b58"},"content":{"rendered":"<h2>Rubular<\/h2>\n<p>Ruby\u3067\u6b63\u898f\u8868\u73fe\u3092\u4f5c\u308b\u305f\u3081\u306e\u30c6\u30b9\u30c8\u306b\u5f79\u7acb\u3064\u30b5\u30a4\u30c8\u3067\u3059\u3002\u4f5c\u3063\u305f\u6b63\u898f\u8868\u73fe\u3092\u305d\u306e\u5834\u3067\u5b9f\u884c\u3057\u3066\u52d5\u4f5c\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>https:\/\/rubular.com\/<\/p>\n<h2>Regexp<\/h2>\n<p>\u6b63\u898f\u8868\u73fe\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002<\/p>\n<h2>Ruby\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u5dee\u7570\u306b\u6ce8\u610f<\/h2>\n<p>Ruby\u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u3088\u3063\u3066<span style=\"color: #ff0000;\"><strong>\u6b63\u898f\u8868\u73fe\u306e\u4ed5\u69d8\u306b\u5fae\u5999\u306b\u5dee\u7570\u304c\u3042\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<p>Ruby2.6\u3067\u3042\u308c\u3070\u4e0b\u8a18\u306b\u8a73\u3057\u304f\u6b63\u898f\u8868\u73fe\u306e\u4ed5\u69d8\u304c\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u78ba\u8a8d\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>https:\/\/docs.ruby-lang.org\/ja\/2.6.0\/doc\/spec=2fregexp.html<\/p>\n<h2>\u6b63\u898f\u8868\u73fe\u306e\u6587\u6cd5<\/h2>\n<h3>=~<\/h3>\n<p>\u30de\u30c3\u30c1\u3057\u305f\u5834\u5408\u306f\u300c\u4e00\u81f4\u3057\u305f\u7b87\u6240\u300d\u3001\u30de\u30c3\u30c1\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u300cnil\u300d\u304c\u8fd4\u308a\u307e\u3059\u3002\u6761\u4ef6\u5206\u5c90\u306e\u6761\u4ef6\u306b\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">'\u6587\u5b57\u5217' =~ \/\u6b63\u898f\u8868\u73fe\/<\/pre>\n<h3>!~<\/h3>\n<p>\u300c=~\u300d\u3068\u306f\u9006\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u30de\u30c3\u30c1\u3057\u306a\u304b\u3063\u305f\u3089true\u3092\u8fd4\u3057\u3001\u30de\u30c3\u30c1\u3057\u305f\u3089false\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>match\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u30ad\u30e3\u30d7\u30c1\u30e3()<\/strong><\/span>\u3054\u3068\u306b\u5206\u89e3\u3057\u3066\u6587\u5b57\u5217\u3092\u62bd\u51fa\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">nengetu = '1987\u5e747\u670829\u65e5'\r\nm = \/(\\d+)\u5e74(\\d+)\u6708(\\d+)\u65e5\/.match(nengetu)\r\nputs m[0] # \u5168\u4f53\u3092\u8868\u793a\uff081987\u5e747\u670829\u65e5\uff09\r\nputs m[1] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e1\u756a\u76ee\u3092\u8868\u793a(1987)\r\nputs m[2] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e2\u756a\u76ee\u3092\u8868\u793a(7)\r\nputs m[3] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e3\u756a\u76ee\u3092\u8868\u793a(29)<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30b7\u30f3\u30dc\u30eb\u3067\u540d\u524d\u3092\u6307\u5b9a\u3057\u3066\u30ad\u30e3\u30d7\u30c1\u30e3\u306e\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">nengetu = '1987\u5e747\u670829\u65e5'\r\nm = \/(?&lt;nen&gt;\\d+)\u5e74(?&lt;getu&gt;\\d+)\u6708(?&lt;hi&gt;\\d+)\u65e5\/.match(nengetu)\r\nputs m[0] # \u5168\u4f53\u3092\u8868\u793a\uff081987\u5e747\u670829\u65e5\uff09\r\nputs m[:nen] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e1\u756a\u76ee\u3092\u8868\u793a(1987)\r\nputs m[:getu] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e2\u756a\u76ee\u3092\u8868\u793a(7)\r\nputs m[:hi] # \u30ad\u30e3\u30d7\u30c1\u30e3\u306e3\u756a\u76ee\u3092\u8868\u793a(29)<\/pre>\n<h3>String\u30af\u30e9\u30b9\u306e\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>String\u30af\u30e9\u30b9\u306b\u306f\u6b63\u898f\u8868\u73fe\u306b\u5bfe\u5fdc\u3057\u305f\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>scan\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3057\u305f\u6587\u5b57\u3092\u914d\u5217\u3067\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">'\u3042 \u3044 \u3046'.scan(\/\\S\/)<\/pre>\n<p>\u4e0a\u8a18\u4f8b\u3067\u8a00\u3048\u3070['\u3042','\u3044','\u3046']\u3068\u3044\u3046\u914d\u5217\u304c\u623b\u308a\u5024\u3068\u3057\u3066\u8fd4\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>[]<\/h3>\n<p>\u6587\u5b57\u5217\u304b\u3089\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u3092\u629c\u304d\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">\u6587\u5b57\u5217[\/\u6b63\u898f\u8868\u73fe\/]<\/pre>\n<h3>split\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>split\u306b\u6b63\u898f\u8868\u73fe\u3092\u6e21\u3059\u3068\u30de\u30c3\u30c1\u3057\u305f\u6587\u5b57\u5217\u3092\u533a\u5207\u308a\u306b\u3057\u3066\u914d\u5217\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\uff08\u3082\u3061\u308d\u3093\u3001\u30ab\u30f3\u30de\u7b49\u306e\u6b63\u898f\u8868\u73fe\u3067\u306f\u306a\u3044\u5358\u7d14\u306a\u6587\u5b57\u306e\u307f\u3092\u6e21\u3059\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"lang:default decode:true \">\u6587\u5b57\u5217.split(\/\u6b63\u898f\u8868\u73fe\/)<\/pre>\n<h3>gsub\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u7b2c\u4e00\u5f15\u6570\u3067\u30de\u30c3\u30c1\u3057\u305f\u6587\u5b57\u3092\u7b2c\u4e8c\u5f15\u6570\u306e\u6587\u5b57\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">\u6587\u5b57\u5217.gsub(\u7b2c\u4e00\u5f15\u6570,\u7b2c\u4e8c\u5f15\u6570)<\/pre>\n","protected":false},"excerpt":{"rendered":"Rubular Ruby\u3067\u6b63\u898f\u8868\u73fe\u3092\u4f5c\u308b\u305f\u3081\u306e\u30c6\u30b9\u30c8\u306b\u5f79\u7acb\u3064\u30b5\u30a4\u30c8\u3067\u3059\u3002\u4f5c\u3063\u305f\u6b63\u898f\u8868\u73fe\u3092\u305d\u306e\u5834\u3067\u5b9f\u884c\u3057\u3066\u52d5\u4f5c\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002 https:\/\/rubular.com\/ Regexp \u6b63\u898f\u8868\u73fe\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002 Ruby\u306e [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7029"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7029"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7029\/revisions"}],"predecessor-version":[{"id":20477,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7029\/revisions\/20477"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7029"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}