Newer
Older
web / info / Regular_expression.html
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta http-equiv="content-type" charset="utf-8">
  <title>正規表現説明シート</title>
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP&display=swap" rel="stylesheet">
  <link href="https://use.fontawesome.com/releases/v5.15.1/css/all.css" rel="stylesheet">
  <link href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="../jdmain.css">
  <script type="text/javascript" src="../jdinit.js"></script>
  <style>
    p {
      font-size: 1.1rem;
      margin-bottom: 10px;
    }

    .program {
      color: White;
      background-color: black;
      font-size: 20px;
      margin-bottom: 1em;
    }

    td {
      text-align: center;
      font-weight: bold;
    }

    .red {
      color: rgb(255, 70, 70);
      font-size: 1.6rem;
    }

    .orengi {
      position: relative;
      background: linear-gradient(transparent 40%, #ffc58f 40%);
    }

    .explain {
      font-size: 1.3rem;
      padding-top: 10px;
      padding-bottom: 30px;
      margin: 0 auto;
      line-height: 1.7;
      font-size: 1.2rem;
    }

    h3 {
      font-size: 1.4rem;
      color: orange;
      margin-bottom: 30px;
      margin-top: 30px;
    }

    .icon {
      margin-left: 40px;
      margin-top: 30px;
      margin-bottom: 30px;
    }

    .black {
      background-color: black;
      color: white;
    }

    .csv-li {
      list-style: decimal;
      text-align: left;
      font-size: 1.2rem;
    }

    .explain-list {
      color: #000;
    }

    .explain-list:hover {
      color: blue;
    }

    .comment {
      color: greenyellow;
    }

    .blue {
      color: rgb(0, 255, 255);
    }

    @media screen and (max-width: 746px) {
      p {
        font-size: 0.8rem;
      }

      .red {
        font-size: 1.2rem;
      }

      .explain {
        font-size: 1.0rem;
        padding-bottom: 30px;
      }

      h3 {
        font-size: 1.2rem;
        margin-bottom: 20px;
        margin-top: 20px;
      }

      .icon {
        margin-top: 20px;
        margin-bottom: 20px;
      }

      li {
        padding: 4px;
        font-size: 1.0rem;
      }

      .section-title::before {
        background-image: none;
        vertical-align: baseline;
        content: '';
        position: absolute;
        bottom: -20px;
        display: inline-block;
        width: 60px;
        height: 5px;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
        background-color: rgb(255, 153, 0);
      }

      .section-title::after {
        display: none;
      }

      .csv-li {
        font-size: 0.8rem;
      }
    }
  </style>
</head>

<body>
  <header id="header">この部分は header.html に置き換わる</header>
  <div class="wrapper">
    <h1 class="section-title">正規表現教材</h1>
    <section class="csv-list">
      <ol class="csv-ol">
        <a href="#about" class="explain-list">
          <li class="csv-li">正規表現について</li>
        </a>
        <a href="#meta-str" class="explain-list">
          <li class="csv-li">正規表現のメタ文字について</li>
        </a>
        <a href="#program" class="explain-list">
          <li class="csv-li">例題プログラム</li>
        </a>
      </ol>
    </section>

    <section id="about">
      <h2 class="section-title">正規表現について</h2>
      <p class="explain">データから文字・文字列を検索するときにそれらのパターンを指定する一般的な表現方法を<span class="red">正規表現</span>といいます。<br>
        調べたい文字・文字列が正規表現によってすぐに検索することができます。<br><br>
        例えば、i(小文字)が含まれる文字・文字列の場合以下のように検索されます。<br>
        <b><span style="color: red;">○</span></b>:i, programming, Application(小文字のiが含まれるので検索される。)<br>
        <b><span sytle="color: blue;">×</span></b>:I, Image(大文字のIなので検索されない。)</p>
      <h3>moji.csv</h3>
      <div class="black">
        <p style="padding-left: 10px;">moji<br>
          i<br>
          programming<br>
          Application<br>
          I<br>
          Image<br></p>
      </div>
      <h3>kensaku1.rb</h3>
      <div class="black">
        <p style="padding-left: 10px;"><span class="blue">require</span> 'csv'<br>
          <br>
          data = CSV.read("moji.csv", headers: true)<br>
          data<span class="blue">.each do</span> |row|<br>
           <span class="blue">if</span> /i/ =~ row["moji"]<br>
            puts row["moji"]<br>
           <span class="blue">end</span><br>
          <span class="blue">end</span>
        </p>
      </div>
      <p>プログラムの解説<br>
        正規表現:/(スラッシュ)で囲みます。<br>
        =~:正規表現と文字列のマッチを行います。</p>
    </section>

    <section id="meta-str">
      <h2 class="section-title">正規表現のメタ文字について</h2>
      <p class="explain">文字・文字列以外に正規表現内で特殊な働きをする文字(メタ文字)で検索することができます。<br><br>
        例えば</p>
      <ul class="explain">
        <li>同じ文字が1回以上続くものを取り出したい(はああい → はあ+い)</li>
        <li>数字のみのデータを取り出したい(2024年 → \d+年)</li>
      </ul>
      <p class="explain">というようにメタ文字を組み合わせることで複雑なデータを検索することができます。<br>
        ※+と\dについては次の「主なメタ表一覧」を見て、マッチする文字列は何なのか、どのような例があるのか確認してみましょう。</p>
      <h3>主なメタ文字一覧</h3>
      <blockquote>
        <table border="1" width="500" height="100">
          <tr>
            <td><b>メタ文字</b></td>
            <td><b>マッチする文字列</b></td>
          </tr>
          <tr>
            <th>.</th>
            <th>なんでもいい1文字(改行を含まない)</th>
          </tr>
          <tr>
            <th>*</th>
            <th>直前の文字0回以上の繰り返し</th>
          </tr>
          <tr>
            <th>+</th>
            <th>直前の文字1回以上の繰り返し</th>
          </tr>
          <tr>
            <th>?</th>
            <th>直前の文字0回もしくは1回の繰り返し</th>
          </tr>
          <tr>
            <th>文字列1|文字列2</th>
            <th>どちらかにマッチすれば良い</th>
          </tr>
          <tr>
            <th>\</th>
            <th>直後のメタ文字を文字としてマッチさせる</th>
          </tr>
          <tr>
            <th>\s</th>
            <th>空白文字(スペース、タブ、改行など)</th>
          </tr>
          <tr>
            <th>\S</th>
            <th>空白文字以外</th>
          </tr>
          <tr>
            <th>\d</th>
            <th>0〜9の数字</th>
          </tr>
          <tr>
            <th>\D</th>
            <th>0〜9の数字以外</th>
          </tr>
        </table>
      </blockquote>

      <h3>主なメタ文字のマッチ例一覧</h3>
      <blockquote>
        <table border="1" width="500" height="100">
          <tr>
            <td>メタ文字</td>
            <td>正規表現</td>
            <td>マッチする</td>
            <td>マッチしない</td>
          </tr>
          <tr>
            <th>.</th>
            <th>/o.k/</th>
            <th>book</th>
            <th>back</th>
          </tr>
          <tr>
            <th>*</th>
            <th>/.*on/</th>
            <th>nippon</th>
            <th>japan</th>
          </tr>
          <tr>
            <th>+</th>
            <th>/-+/</th>
            <th>o-------i</th>
            <th>oi</th>
          </tr>
          <tr>
            <th>?</th>
            <th>/ts?u/</th>
            <th>tsu, tu</th>
            <th>su</th>
          </tr>
          <tr>
            <th>文字列1|文字列2</th>
            <th>/cat|dog/</th>
            <th>cat, dog</th>
            <th>fox</th>
          </tr>
          <tr>
            <th>\</th>
            <th>/\+/</th>
            <th>10+1</th>
            <th>10-1</th>
          </tr>
          <tr>
            <th>\s</th>
            <th>/e\sa/</th>
            <th>We are</th>
            <th>Weare</th>
          </tr>
          <tr>
            <th>\S</th>
            <th>/a\Su/</th>
            <th>aisatu</th>
            <th>aisa tu</th>
          </tr>
          <tr>
            <th>\d</th>
            <th>/\dsai/</th>
            <th>15sai</th>
            <th>juugosai</th>
          </tr>
          <tr>
            <th>\D</th>
            <th>\/Dsai/</th>
            <th>Juugosai</th>
            <th>15sai</th>
          </tr>
        </table>
      </blockquote>
    </section>

    <section id="program">
      <h2 class="section-title">例題プログラム</h2>
      <h3>kensaku.csv</h3>
      <div class="black">
        <p style="padding-left: 10px;">name,gakko,gakunen,kozukai<br>
          公益太郎,三川八中,2,2000<br>
          飯森花子,余目百中,1,4000<br>
          鶴岡一人,タキタロウ小,4,200<br>
          鶴岡二子,タキタロウ小,2,300<br>
          遊佐梅子,丸池小,6,900</p>
      </div>

      <h3>kensaku2.rb</h3>
      <div class="black">
        <p style="padding-left: 10px;"><span class="blue">require</span> 'csv'<br>
          <br>
          data = CSV.read("kensaku.csv", headers: true)<br>
          puts "学校で探す:1"<br>
          puts "金額で探す:2"<br>
          print "どれにしますか:"<br>
          sel = gets.to_i<br>
          <br>
          <span class="blue">if</span> sel == 1<br>
           print "探したい学校名を入れてください:"<br>
           ptn = Regexp.new(gets.chomp)<br>
           <span class="comment">#gets.chompで入力された文字列をRegexp.newにより正規表現に直す</span><br>
           data<span class="blue">.each do</span> |row|<br>
            <span class="blue">if</span> ptn =~ row["gakko"]<br>
             printf("%sさんは%s%s年です\n", row["name"], row["gakko"], row["gakunen"])<br>
            <span class="blue">end</span><br>
           <span class="blue">end</span><br>
          <span class="blue">elsif</span> sel == 2<br>
           print "絞り込みしたい金額を入れてください:"<br>
           gaku = gets.to_i<br>
           data<span class="blue">.each do</span> |row|<br>
            <span class="blue">if</span> gaku < row["gozukai"].to_i<br>
             printf("%sさんは%d円もらっています\n", row["name"], row["kozukai"].to_i)<br>
            <span class="comment">#csvから読んできたデータは全部文字列</span><br>
            <span class="comment">#数字で比較するときは.to_iや.to_fを使う</span><br>
            <span class="blue">end</span><br>
           <span class="blue">end</span><br>
          <span class="blue">else</span><br>
           printf("%d番はありません。さようなら。\n", sel)<br>
          <span class="blue">end</span><br>
        </p>
      </div>
    </section>

    <p style="color: green; font-size: 1.5rem; font-weight: bold;text-align: center;margin-top: 40px;">
      正規表現を使ったプログラムを作成してみよう!!</p>

  </div>
  <footer id="footer">この部分は footer.html に置き換わる</footer>

</body>

</html>