翻訳への提案を行います
 
他のユーザーによる提案:

progress indicator
他の提案はありません。
クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
Visual Studio 2010
Visual Studio
Visual Studio の言語
JScript
正規表現の概説
 アンカー
すべて縮小/すべて展開 すべて縮小
コンテンツの表示:   英語と日本語を並べて表示コンテンツの表示: 英語と日本語を並べて表示
この内容は機械翻訳によるものです。
JScript 10.0
Anchors

Anchors enable you to fix a regular expression to the start or end of a line or input string. They also enable you to create expressions that match the start, end, or interior of a word.

For example, in the expression er\b, the \b matches a word boundary. The expression matches the "er" in "never", but not the "er" in "verb".

The following table contains the list of regular expression anchors and their meanings:

Character

Description

^

Matches the position at the beginning of the input string. If the m (multiline search) character is included with the flags, ^ also matches the position following \n or \r.

$

Matches the position at the end of the input string. If the m (multiline search) character is included with the flags, $ also matches the position preceding \n or \r.

\b

Matches a word boundary, that is, the position between a word and a space.

\B

Matches a nonword boundary.

You cannot use a quantifier with an anchor. Since you cannot have more than one position immediately before or after a newline or word boundary, expressions such as ^* are not permitted.

To match text at the beginning of a line of text, use the ^ character at the beginning of the regular expression. Do not confuse this use of the ^ with the use within a bracket expression.

To match text at the end of a line of text, use the $ character at the end of the regular expression.

To use anchors when searching for chapter headings, the following regular expression matches a chapter heading that contains no more than two following digits and that occurs at the beginning of a line:

JScript
/^Chapter [1-9][0-9]{0,1}/

Not only does a true chapter heading occur at the beginning of a line, it is also the only text on the line. It occurs at beginning of the line and also at the end of the same line. The following expression ensures that the specified match only matches chapters and not cross-references. It does so by creating a regular expression that matches only at the beginning and end of a line of text.

JScript
/^Chapter [1-9][0-9]{0,1}$/

Matching word boundaries is a little different but adds a very important capability to regular expressions. A word boundary is the position between a word and a space. A nonword boundary is any other position. The following expression matches the first three characters of the word "Chapter" because the characters appear following a word boundary:

JScript
/\bCha/

The position of the \b operator is critical. If it is at the beginning of a string to be matched, it looks for the match at the beginning of the word. If it is at the end of the string, it looks for the match at the end of the word. For example, the following expression matches the string "ter" in the word "Chapter" because it appears before a word boundary:

JScript
/ter\b/

The following expression matches the string "apt" as it occurs in "Chapter" but not as it occurs in "aptitude":

JScript
/\Bapt/

The string "apt" occurs on a nonword boundary in the word "Chapter" but on a word boundary in the word "aptitude". For the \B nonword boundary operator, position is not important because the match is not relative to the beginning or end of a word.

JScript 10.0
アンカー

アンカーを使用すると、正規表現の一致箇所を行頭または行末あるいは入力文字列に固定できます。 また、単語の先頭、単語の末尾、または単語内と一致する正規表現を作成することもできます。

たとえば、正規表現の er\b では、\b はワード境界と一致します。 この正規表現は、"never" の "er" とは一致しますが、"verb" の "er" とは一致しません。

正規表現のアンカーとその意味を次の表に示します。

文字

説明

^

入力文字列の先頭と一致します。 m (複数行検索) 文字のフラグが設定されている場合、^ は \n または \r の直後とも一致します。

$

入力文字列の末尾と一致します。 m (複数行検索) 文字のフラグが設定されている場合、$ は \n または \r の直前とも一致します。

\b

ワード境界と一致します。ワード境界とは、単語と空白との間の位置のことです。

\B

ワード境界以外と一致します。

量指定子とアンカーは一緒に使用できません。 改行またはワード境界の直前または直後の位置は 1 つに限られるため、^* などの正規表現は使用できません。

行頭の文字列と一致させるには、正規表現の先頭で ^ を指定します。 角かっこ内で使用する ^ とは異なるため、間違えないようにしてください。

行末の文字列と一致させるには、正規表現の最後で $ を指定します。

アンカーを使用した次の正規表現で章見出しを検索すると、行頭から始まる章見出しのうち、章番号が 2 桁までのものと一致します。

JScript
/^Chapter [1-9][0-9]{0,1}/

章見出しは行の先頭であるだけでなく、行全体が章見出しとなります。 行の先頭であると同時に、同じ行の行末にもなります。 次の正規表現では、参照を除外した、実際の章見出しだけに一致します。 行の先頭と最後だけに一致するように正規表現で指定しています。

JScript
/^Chapter [1-9][0-9]{0,1}$/

ワード境界と一致させる場合は少し異なりますが、正規表現では重要な機能です。 ワード境界とは、単語と空白との間の位置のことです。 それ以外の位置は、ワード境界ではありません。 次の正規表現は、"Chapter" の先頭の 3 文字と一致します。これらの文字は、ワード境界の直後に出現するためです。

JScript
/\bCha/

\b 演算子の位置が重要になります。 一致させる文字列の先頭に演算子を指定した場合は、単語の先頭で一致する部分を検索します。 演算子を文字列の末尾に指定した場合は、単語の末尾で一致する部分を検索します。 たとえば、次の正規表現は、"Chapter" の文字列 "ter" と一致します。この文字列は、ワード境界の前に出現するためです。

JScript
/ter\b/

次の正規表現は、"Chapter" の文字列 "apt" と一致しますが、"aptitude" の "apt" とは一致しません。

JScript
/\Bapt/

文字列 "apt" は、"Chapter" ではワード境界以外の位置にありますが、"aptitude" ではワード境界にあります。 ワード境界以外の位置を指定する \B 演算子では、単語の先頭または末尾を基準にしないため、演算子を指定する位置は重要ではありません。

その他の技術情報

コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2012 Microsoft. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker