Skip to content

Japanese translation of regex1 #887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions docs/labs/ja_regex1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<!DOCTYPE html>
<html lang="ja">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css">
<link rel="stylesheet" href="checker.css">
<script src="checker.js"></script>
<script src="regex1.js"></script>
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">

<!-- See create_labs.md for how to create your own lab! -->

</head>
<body>
<!-- For GitHub Pages formatting: -->
<div class="container-lg px-3 my-5 markdown-body">
<h1>ラボ演習 regex1</h1>
<p>
これはセキュアなソフトウェア開発に関するラボ演習です。
ラボの詳細については、<a href="introduction.html" target="_blank">概要</a>をご覧ください。

<p>
<h2>ゴール</h2>
<p>
<b>入力検証のためのシンプルな正規表現の書き方を学びます。</b>

<p>
<h2>背景</h2>
<p>
正規表現(regular expressions: regexes)はテキストのパターンを表現するために広く利用されている表記方法です。
正規表現は入力の検証に使うことができます。正しく使用することで、多くの攻撃への対策となります。

<p>
正規表現の表記方法は言語により若干異なりますが、大枠では共通しています。以下が正規表現を表記するための基本的なルールです。

<ol>
<li>最も単純なルールは、文字や数字はそれ自身にマッチするということです。つまり、"<tt>d</tt>" という正規表現は "<tt>d</tt>" という文字にマッチします。多くの実装
ではデフォルトで大文字・小文字を区別してマッチし、それは通常望まれる動作です。
<li>もうひとつのルールは、いくつかの文字のうちどれかを指定するために大カッコで囲むというものです。もし大カッコが単なる英数字を囲っていた場合は、それらは囲まれた英数字のどれかにマッチします。つまり <tt>[brt]</tt> は単一の文字である "<tt>b</tt>", "<tt>r</tt>", "<tt>t</tt>" のどれかにマッチします。
大カッコの中ではダッシュ("-")で文字の範囲を示すことができます。つまり <tt>[A-D]</tt> はその範囲の一文字、すなわち一文字の A、一文字の B、一文字の C、一文字の D のどれかにマッチします。
大カッコの中には複数の範囲を書くことができます。
例えば <tt>[A-Za-z]</tt> は、大文字のアルファベット一文字または小文字のアルファ
ベット一文字にマッチします。
(このラボでは EBCDIC のような使われなくなって久しい文字システムを使用していないと仮定しています)
<li>パターンに続けて "<tt>&#42;</tt>" が書かれた場合、それは"<i>0回以上</i>"を意味します。
ほとんど全ての正規表現の実装では(ただし POSIX BRE を除く)、パターンに続けて "<tt>+</tt>" が書かれた場合は"<i>1回以上</i>"を意味します。
つまり <tt>[A-D]*</tt> は、A, B, C, D のどれかが0文字以上続く場合にマッチします
<li>選択肢、つまり指定したどれかにマッチするためには、"<tt>|</tt>" を使うことができます。
"<tt>|</tt>" は優先度が低いので、入力の検証に用いる場合は正しい選択肢を小カッコで囲む必要があります。つまり、例えば "yes" または "no" にマッチさせる方法は "<tt>(yes|no)</tt>" です。
</ol>

<p>
<h2>タスクの詳細</h2>
<p>
正規表現を使って入力の<i>検証</i>を行いたいとします。
それはつまり、入力は正規表現のパターンに<i>完全に</i>一致するということです。
正規表現では、シンボルを前置したり後置したりして、(マルチラインモードではなく)デフォルトモードでそれを行うことができます。
残念ながら、異なるプラットフォームでは入力に完全一致させるために異なる正規表現のシンボルを使用します。
以下の表は様々なプラットフォーム(で使用されるデフォルトの正規表現システム)において、どんな前置や後置が必要かを簡単に示したものです。

<p>
<table>
<tr>
<th>プラットフォーム
<th>前置
<th>後置
<tr>
<td>POSIX BRE, POSIX ERE, and ECMAScript (JavaScript)
<td>“^”
<td>“$”
<tr>
<td>Java, .NET, PHP, Perl, and PCRE
<td>“^” or “\A”
<td>“\z”
<tr>
<td>Golang, Rust crate regex, and RE2
<td>“^” or “\A”
<td>“$” or “\z”
<tr>
<td>Python
<td>“^” or “\A”
<td>“\Z” (not “\z”)
<tr>
<td>Ruby
<td>“\A”
<td>“\z”
</table>

<p>
例えば ECMAScript (JavaScript) で入力を "<tt>ab</tt>" または "<tt>de</tt>" としたい場合に使用する正規表現は "<tt>^(ab|de)$</tt>" になります。
同じことを Python で行う場合は "<tt>^(ab|de)\Z</tt>" または "<tt>\A(ab|de)\Z</tt>" となります。(正規表現のパターンが少し違うことに注意してください)

<p>
より詳しい情報は OpenSSF ガイド <a href="https://best.openssf.org/Correctly-Using-Regular-Expressions">Correctly Using Regular Expressions for Secure Input Validation</a> にあります。

<p>
<h2>演習 (<span id="grade"></span>)</h2>

<b>
以下の要求を満たす正規表現(regex)のパターンを作成してください。
</b>

<p>
必要に応じて「ヒント」や「諦める」のボタンを押してください。

<h3>Part 1</h3>
<p>
ECMAScript (JavaScript) で使うことを想定して、文字 "Y" または "N" のみにマッチする正規表現を作成してください。
<!--
You can use this an example for new labs.
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
<textarea id="attempt" rows="2" cols="65">...</textarea>
-->
<form id="lab1"><pre><code
><input id="attempt0" type="text" size="60" spellcheck="false" value="">
</code></pre>
<button type="button" class="hintButton">ヒント</button>
<button type="button" class="resetButton">リセット</button>
<button type="button" class="giveUpButton">諦める</button>
</form>

<h3>Part 2</h3>
<p>
ECMAScript (JavaScript) で使うことを想定して、1文字以上の大文字のアルファベット(A から Z)にのみにマッチする正規表現を作成してください。
<!--
You can use this an example for new labs.
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
<textarea id="attempt" rows="2" cols="65">...</textarea>
-->
<form id="lab2">
<pre><code
><input id="attempt1" type="text" size="60" spellcheck="false" value="">
</code></pre>
<button type="button" class="hintButton">ヒント</button>
<button type="button" class="resetButton">リセット</button>
<button type="button" class="giveUpButton">諦める</button>
</form>

<h3>Part 3</h3>
<p>
ECMAScript (JavaScript) で使うことを想定して、単語 "true" または "false" のみにマッチする正規表現を作成してください。
<!--
You can use this an example for new labs.
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
<textarea id="attempt" rows="2" cols="65">...</textarea>
-->
<form id="lab3">
<pre><code
><input id="attempt2" type="text" size="60" spellcheck="false" value="">
</code></pre>
<button type="button" class="hintButton">ヒント</button>
<button type="button" class="resetButton">リセット</button>
<button type="button" class="giveUpButton">諦める</button>
</form>

<h3>Part 4</h3>
<p>
1文字以上の大文字のアルファベット(A から Z)にのみにマッチする正規表現を作成してください。
ただし今回は、(JavaScript ではなく)Python で使うことを想定してください。
<!--
You can use this an example for new labs.
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
<textarea id="attempt" rows="2" cols="65">...</textarea>
-->
<form id="lab4">
<pre><code
><input id="attempt3" type="text" size="60" spellcheck="false" value="">
</code></pre>
<button type="button" class="hintButton">ヒント</button>
<button type="button" class="resetButton">リセット</button>
<button type="button" class="giveUpButton">諦める</button>
</form>

<h3>Part 5</h3>
<p>
1文字の大文字のアルファベット(A から Z)に続けてダッシュ ("-")、さらに続けて1文字以上の数字にのみにマッチする正規表現を作成してください。
今回は、(JavaScript ではなく)Ruby で使うことを想定してください。
<!--
You can use this an example for new labs.
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
<textarea id="attempt" rows="2" cols="65">...</textarea>
-->
<form id="lab5">
<pre><code
><input id="attempt4" type="text" size="60" spellcheck="false" value="">
</code></pre>
<button type="button" class="hintButton">ヒント</button>
<button type="button" class="resetButton">リセット</button>
<button type="button" class="giveUpButton">諦める</button>
</form>

<br><br>
<p>
<i>このラボは、<a href="https://www.linuxfoundation.org/">Linux Foundation</a>のDavid A. Wheelerによって開発されました。</i>
<br><br>
<p id="correctStamp" class="small">
<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly>
</textarea>

</div><!-- End GitHub pages formatting -->
</body>
</html>
Loading