|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="ja"> |
| 3 | +<head> |
| 4 | +<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | +<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css"> |
| 7 | +<link rel="stylesheet" href="checker.css"> |
| 8 | +<script src="checker.js"></script> |
| 9 | +<script src="regex0.js"></script> |
| 10 | +<link rel="license" href="https://creativecommons.org/licenses/by/4.0/"> |
| 11 | + |
| 12 | +<!-- See create_labs.md for how to create your own lab! --> |
| 13 | + |
| 14 | +</head> |
| 15 | +<body> |
| 16 | +<!-- For GitHub Pages formatting: --> |
| 17 | +<div class="container-lg px-3 my-5 markdown-body"> |
| 18 | +<h1>ラボ演習 regex0</h1> |
| 19 | +<p> |
| 20 | +これはセキュアなソフトウェア開発に関するラボ演習です。 |
| 21 | +ラボの詳細については、<a href="introduction.html" target="_blank">概要</a>をご覧ください。 |
| 22 | + |
| 23 | +<p> |
| 24 | +<h2>ゴール</h2> |
| 25 | +<p> |
| 26 | +<b> |
| 27 | +シンプルな正規表現の書き方を学びます。 |
| 28 | +</b> |
| 29 | + |
| 30 | +<p> |
| 31 | +<h2>背景</h2> |
| 32 | +<p> |
| 33 | +正規表現(regular expressions: regexes)はテキストのパターンを表現するために広く利用されている表記方法です。 |
| 34 | +ここでは入力検証のための正規表現の使い方を見ていきます。 |
| 35 | + |
| 36 | +<p> |
| 37 | +正規表現の表記方法は言語により若干異なりますが、大枠では共通しています。以下が正規表現を表記するための基本的なルールです。 |
| 38 | + |
| 39 | +<ol> |
| 40 | +<li>最も単純なルールは、文字や数字はそれ自身にマッチするということです。つまり、"<tt>d</tt>" という正規表現は "<tt>d</tt>" という文字にマッチします。多くの実装ではデフォルトで大文字・小文字を区別してマッチし、それは通常望まれる動作です。 |
| 41 | +<li>もうひとつのルールは、いくつかの文字のうちどれかを指定するために角カッコで囲むというものです。もし角カッコが単なる英数字を囲っていた場合は、それらは囲まれた英数字のどれかにマッチします。つまり <tt>[brt]</tt> は単一の文字である "<tt>b</tt>", "<tt>r</tt>", "<tt>t</tt>" のどれかにマッチします。 |
| 42 | +カッコの中ではダッシュ("-")で文字の範囲を示すことができます。つまり <tt>[A-D]</tt> はその範囲の一文字、すなわち一文字の A、一文字の B、一文字の C、一文字の D のどれかにマッチします。 |
| 43 | +カッコの中には複数の範囲を書くことができます。 |
| 44 | +例えば <tt>[A-Za-z]</tt> は、大文字のアルファベット一文字または小文字のアルファベット一文字にマッチします。 |
| 45 | +(このラボでは EBCDIC のような使われなくなって久しい文字システムを使用していないと仮定しています) |
| 46 | +<li>パターンに続けて "<tt>*</tt>" が書かれた場合、それは"<i>0回以上</i>"を意味します。 |
| 47 | +ほとんど全ての正規表現の実装では(ただし POSIX BRE を除く)、パターンに続けて "<tt>+</tt>" が書かれた場合は"<i>1回以上</i>"を意味します。 |
| 48 | +つまり <tt>[A-D]*</tt> は、A, B, C, D のどれかが0文字以上続く場合にマッチします。 |
| 49 | +</ol> |
| 50 | + |
| 51 | +<p> |
| 52 | +<h2>タスクの詳細</h2> |
| 53 | +<p> |
| 54 | +特定のパターンを検索するためのシンプルな正規表現(regular expression: regexes)を書いてみましょう。 |
| 55 | +必要に応じて「ヒント」や「諦める」のボタンを押してください。 |
| 56 | + |
| 57 | +<p> |
| 58 | +<h2>演習 (<span id="grade"></span>)</h2> |
| 59 | + |
| 60 | +<b> |
| 61 | +以下の要求を満たす正規表現(regex)のパターンを作成してください。 |
| 62 | +</b> |
| 63 | + |
| 64 | +<h3>Part 1</h2> |
| 65 | +<p> |
| 66 | +ECMAScript (JavaScript) で使うことを想定して、"cat" という語句が文のどこにあるかに関わらず検索するための正規表現を作成してください。 |
| 67 | +<!-- |
| 68 | +You can use this an example for new labs. |
| 69 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 70 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 71 | +--> |
| 72 | +<form id="lab1"><pre><code |
| 73 | +><input id="attempt0" type="text" size="60" spellcheck="false" value=""> |
| 74 | +</code></pre> |
| 75 | +<button type="button" class="hintButton">ヒント</button> |
| 76 | +<button type="button" class="resetButton">リセット</button> |
| 77 | +<button type="button" class="giveUpButton">諦める</button> |
| 78 | +</form> |
| 79 | + |
| 80 | +<h3>Part 2</h2> |
| 81 | +<p> |
| 82 | +ECMAScript (JavaScript) で使うことを想定して、1つ以上の "A" と1つ以上の "B" が連続する箇所を検索するための正規表現を作成してください。 |
| 83 | +<!-- |
| 84 | +You can use this an example for new labs. |
| 85 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 86 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 87 | +--> |
| 88 | +<form id="lab2"> |
| 89 | +<pre><code |
| 90 | +><input id="attempt1" type="text" size="60" spellcheck="false" value=""> |
| 91 | +</code></pre> |
| 92 | +<button type="button" class="hintButton">ヒント</button> |
| 93 | +<button type="button" class="resetButton">リセット</button> |
| 94 | +<button type="button" class="giveUpButton">諦める</button> |
| 95 | +</form> |
| 96 | + |
| 97 | +<br><br> |
| 98 | +<p> |
| 99 | +<i>このラボは、<a href="https://www.linuxfoundation.org/">Linux Foundation</a>のDavid A. Wheelerによって開発されました。</i> |
| 100 | +<br><br> |
| 101 | +<p id="correctStamp" class="small"> |
| 102 | +<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly> |
| 103 | +</textarea> |
| 104 | + |
| 105 | +</div><!-- End GitHub pages formatting --> |
| 106 | +</body> |
| 107 | +</html> |
0 commit comments