メインコンテンツまでスキップ

SenseTalkのパターン言語でのアンカー

SenseTalkのパターン言語を使って、テキスト内のパターンに一致するものを見つけることができます。詳細はSenseTalkパターン言語基本をご覧ください。特定のテキストやパターンではなく、テキスト内の位置に基づいてマッチを作成したい場合もあるでしょう。このようなマッチは、位置にパターンマッチやサブパターンをアンカーすることができます。

SenseTalkは、パターン内にアンカーを作成するいくつかの方法を提供します。以下に説明します:

基本的なアンカーの定義

パターン内の要素やサブパターンは、テキストの始まりや終わり、またはテキスト内の行や単語の始まりや終わりにアンカーされることができます。たとえば、次のパターンは、cで始まり、tで終わる任意の全単語(catcoastなど)にマッチしますが、単語内の同じパターン(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 "|"

その結果、テキスト文字列は次のようになります:

|To| |all| |who| |come| |to| |this| |happy| |place|, |welcome|.

コンテキスト内の位置

パターンは、マッチ範囲の一部として扱われない他のパターン要素を使用して位置にアンカーすることができます。このアンカータイプは、特定のマッチ位置を識別し、その位置に対して追加のパターン要素を指定します。

このようなアンカーは、通常、先読みや後読みと呼ばれます。アンカーの後または前に追加のパターン要素をマッチさせることができます。例えば、括弧で囲まれた1つ以上の文字を指定するパターンから始めてみましょう:

< "(" then some characters then ")" >

このパターン定義は、囲んでいる括弧を含むテキストの出現をマッチします。囲む括弧自体なしで囲まれた内容のみを取得するには、preceded by および followed by を使用します:

<preceded by "(" then some characters then followed by ")">

このパターンでは、some characters がマッチに含まれるパターンの唯一の部分です。preceded by "(" は、パターンのマッチを見つけるために文字の前に存在する必要がある開始括弧を特定する後読みアンカーで、括弧自体はマッチに含まれません。

同様に、followed by ")" は、閉じる括弧に続く文字群のみをマッチさせ、その文字自体はマッチに含まれない先読みアンカーです。

同じパターンは、もっと会話的に次のように書くこともできます:

<some characters preceded by "(" followed by ")">

Lookaroundアンカーの構文:

preceded by locatingPattern -- locatingPatternがすぐ前にある位置
not preceded by locatingPattern -- locatingPatternがすぐ前にない位置
followed by locatingPattern -- locatingPatternがすぐ後にある位置
not followed by locatingPattern -- locatingPatternがすぐ後にない位置
pattern preceded by locatingPattern -- locatingPatternが前にある位置のパターン
pattern not preceded by locatingPattern -- locatingPatternが前にない位置のパターン
pattern followed by locatingPattern -- locatingPatternが後にある位置のパターン
pattern not followed by locatingPattern -- locatingPatternが後にない位置のパターン
Important

重要: preceded byと一緒に使用するlocatingPatternは長さが制限されていなければなりません。それは、preceded by some characterspreceded by one or more digitsのように、長さが不定で制限されていないパターンを含むことはできません。preceded by 3 to 5 digitsのように、変数であるが長さが制限されているものは許可されています。followed byと一緒に使用されるパターンにはこの制限はありません。

lookaroundアンカーを使用すると、locatingPatternはソーステキスト内の文字を消費せず、一致範囲の一部ではありませんので、ご注意ください。