SenseTalkのパターン言語でのアンカー
SenseTalkのパターン言語を使って、テキスト内のパターンに一致するものを見つけることができます。詳細はSenseTalkパターン言語基本をご覧ください。特定のテキストやパターンではなく、テキスト内の位置に基づいてマッチを作成したい場合もあるでしょう。このようなマッチは、位置にパターンマッチやサブパターンをアンカーすることができます。
SenseTalkは、パターン内にアンカーを作成するいくつかの方法を提供します。以下に説明します:
基本的なアンカーの定義
パターン内の要素やサブパターンは、テキストの始まりや終わり、またはテキスト内の行や単語の始まりや終わりにアンカーされることができます。たとえば、次のパターンは、cで始まり、tで終わる任意の全単語(catやcoastなど)にマッチしますが、単語内の同じパターン(concatenationの中など)にはマッチしません:
set wordPattern to <word starting with "c", some characters, "t" at the end of the word>
指定された位置でサブパターンをアンカーするためのいくつかの異なる構文形式を使用することができます。
単語アンカー構文:
{a | the} word [starting | beginning] {with} pattern -- 単語の始まり
{a | the} word {that} [starts | begins] {with} pattern -- 単語の始まり
pattern at {the} [start | beginning] of {a | the} word -- 単語の始まり
{a | the} word ending {with} pattern -- 単語の終わり
{a | the} word {that} ends {with} pattern -- 単語の終わり
pattern at {the} [end | ending] of {a | the} word -- 単語の終わり
行アンカー構文:
{a | the} line [starting | beginning] {with} pattern -- 行の始まり
{a | the} line {that} [starts | begins] {with} pattern -- 行の始まり
pattern at {the} [start | beginning] of {a | the} line -- 行の始まり
{a | the} line ending {with} pattern -- 行の終わり
{a | the} line {that} ends {with} pattern -- 行の終わり
pattern at {the} [end | ending] of {a | the} line -- 行の終わり
全テキストアンカー構文:
text [starting | beginning] {with} pattern -- テキストの始まり
text {that} [starts | begins] {with} pattern -- テキストの始まり
pattern at {the} [start | beginning] of {the} text -- テキストの始まり
text ending {with} pattern -- テキストの終わり
text {that} ends {with} pattern -- テキストの終わり
pattern at {the} [end | ending] of {the} text -- テキストの終わり
スタンドアロンアンカー
アンカーを独立してその自身のサブパターンとして指定することができます。このように指定されたアンカーは、指定された位置でマッチしますが、マッチに含まれる文字はありません。つまり、マッチのその部分は空 の文字列です。
スタンドアロンアンカー構文:
{a | the} word break -- 単語の始まりまたは終わりの位置
{a | the} [beginning | start | starting] of {a | the} word -- 単語の始まりの位置
{a | the} word [beginning | start | starting] -- 単語の始まりの位置
{a | an | the} end of {a | the} word -- 単語の終わりの位置
{a | the} word end -- 単語の終わりの位置
{a | the} [beginning | start | starting] of {a | the} line -- 行の始まり
{a | an | the} end of {a | the} line -- 行の終わり
{a | the} line start -- 行の始まりの位置
{a | the} line end -- 行の終わりの位置
{a | the} [beginning | start | starting] of {a | the} text -- テキストの始まり
{a | an | the} end of {a | the} text -- テキストの終わり
{a | the} text start -- テキストの始まりの位置
{a | the} text end -- テキストの終わりの位置
これらのアンカーは、それぞれテキスト内の純粋な位置を表し、文字を含みません。次の例は、単語と非単語文字の間のすべての遷移を探し、その位置に "|" 文字を挿入します:
set sentence to "To all who come to this happy place, welcome."
replace every <word break> in sentence with "|"