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

パターンマッチング関数

SenseTalkのパターン言語では、自然言語を用いて検索に使用できるパターンを定義できます。以下の関数は、パターン言語の定義とともに使用できます。

The matches operator is used to test whether some text matches a pattern:

さらに、以下で説明されている演算子や関数は、パターンとの使用に特化しています。

Match, Every Match 関数

挙動: match関数とevery match関数を用いて、テキスト内のパターンを探すことができます。match関数は指定したパターンの最初の出現を見つけ、every match関数はソース内のパターンのすべての出現を見つけます。

パラメータ:

  • the match of pattern (必須): 角括弧( < ... > )内のパターン言語構文か変数として指定できます。パターン言語構文に関する情報は、Pattern Language Syntaxを参照してください。
  • in source (必須): 引用符で囲まれた文字列、変数、またはテキストを生成する式として指定できます。
  • after position (オプション): テキスト内の文字位置を指定する数値で、パターンマッチの検索が次の文字位置から始まるようにします。
  • before position (オプション): テキスト内の文字位置を指定し、その文字位置の前の文字でパターンマッチの検索が終わるようにします。
  • caseSensitivity (オプション): 標準の大文字小文字の区別フレーズ(caseSensitive, with case, 等)を指定して、テキストの検索が大文字小文字を区別するかどうかを決定します。デフォルト: caseInsensitive

構文:
{the} match of pattern [in | within] source { [before | after] [ {position | location} position | {the} end] } {considering case | ignoring case}
every match of pattern [in | within] source { [before | after] [ {position | location} position | {the}end] } {considering case | ignoring case}

match( pattern, source {, position {, caseSensitive {, treatPositionAsBefore }}} )
everyMatch( pattern, source {, position {, caseSensitive {, treatPositionAsBefore }}} )

ノート

match()またはeveryMatch()の伝統的な関数呼び出し構文を使用する場合、最初の2つのパラメータは必須で、残りの3つはオプションです。この場合の_caseSensitive_パラメータはブール値(デフォルト: False)で、検索が大文字小文字を区別するかどうかを示します。_treatPositionAsBefore_パラメータはブール値(デフォルト: False)で、検索が_position_パラメータで指定された位置の前に行われるべきかどうかを指定します。

返り値: パターンが見つかったソーステキストの位置に関する情報を提供する1つ以上のプロパティリスト。match関数は1つのプロパティリストを返し、every match関数は見つかった各マッチに対してプロパティリストを返します。各マッチのプロパティリストには少なくとも2つのプロパティが含まれます:

  • text: マッチした完全なテキスト。
  • text_range: マッチしたテキストが位置していたソースの文字範囲。

パターンが1つ以上のキャプチャグループを含む場合、マッチプロパティリストには、含まれる各キャプチャグループに対するプロパティのペアも含まれます:

  • 名前:キャプチャグループの名前。
  • 名前_範囲:キャプチャグループが見つかった範囲。
ノート

フルマッチテキストは常にプロパティ名 text で返されるため、パターン内のキャプチャグループに text を名前として使用しないでください。

例:

put the match of <punctuation> within "Green 1: 112-14" --> {text:":", text_range:"8" to "8"}

例:

put the match of <3 digits> in "1bc3 8472QX905" --> {text:"847", text_range:"6" to "8"}

例:

put match(<3 digits>, "1bc3 8472QX905") --> {text:"847", text_range:"6" to "8"}

例:

put every match of <3 digits> in "123456789" --> [{text:"123", text_range:"1" to "3"},{text:"456", text_range:"4" to "6"},{text:"789", text_range:"7" to "9"}]

例:

put everyMatch (<3 digits>, "123456789") --> [{text:"123", text_range:"1" to "3"},{text:"456", text_range:"4" to "6"},{text:"789", text_range:"7" to "9"}]

関連:

Occurrence, Every Occurrence 関数

挙動: occurrenceevery occurrence 関数は、定義されたパターンのマッチしたテキストを返します。 occurrence 関数は最初に見つかったマッチを返し、every occurrence はソース内で見つかったすべてのマッチのリストを返します。

パラメータ:

  • the occurrence of pattern (必須): 角括弧内( < ... > )のパターン言語構文または変数で指定できます。パターン言語構文についての情報は、Pattern Language Syntaxを参照してください。
  • in source (必須): 引用符で囲んだ文字列、変数、またはテキストを生成する表現として指定できます。
  • after position (オプション): テキスト内の文字位置を指定し、その文字位置から次の文字位置でパターンマッチの検索が始まるようにします。
  • before position (オプション): テキスト内の文字位置を指定する数値で、パターンマッチの検索がその文字位置の前の文字で終わるようにします。
  • caseSensitivity (オプション): 標準的なケースセンシティビティフレーズ(caseSensitivewith caseなど)を指定して、テキスト検索がケースセンシティブかどうかを決定します。デフォルト:caseInsensitive

構文:
{the} occurrence of pattern [in | within] source { [before | after] [ {position | location} position | {the} end] } {considering case | ignoring case}
every occurrence of pattern [in | within] source { [before | after] [ {position | location} position | {the} end] } {considering case | ignoring case}

occurrence( pattern, source {, position {, caseSensitive {, treatPositionAsBefore }}} )
everyOccurrence( pattern, source {, position {, caseSensitive {, treatPositionAsBefore }}} )

ノート

SenseTalkスクリプト内では、occurrenceinstanceという言葉は互換的に使用されます。スクリプトでoccurrenceという単語が使用されている場所では、代わりにinstanceという単語を使用することができます。

Tech Talk

occurrence()またはeveryOccurrence()の伝統的な関数呼び出し構文を使用する場合、最初の2つのパラメータは必須で、残りの3つはオプションです。この場合の_caseSensitive_パラメータは、検索が大文字小文字を区別するかどうかを示すブール値(デフォルト:False)です。_treatPositionAsBefore_パラメータはブール値(デフォルト:False)で、検索が_position_パラメータで指定された位置の前に行われるべきかどうかを指定します。

戻り値: パターンと一致するテキスト、存在しない場合は空です。

例:

put the occurrence of <3 digits> in "1bc3 8472QX905" --> "847"

例:

put the occurrence of <"$", digits> in "$895" —> "$8"

例:

put occurrence (<"$", digits>, "$895") —> "$8"

例:

put every occurrence of <3 digits> in "123456789" --> [123,456,789]

例:

put occurrence of <max digits> in "Issue #429 was resolved on 15-Jun-2018" --> 429
put the range of <max digits> in "Issue #429 was resolved on 15-Jun-2018" --> 8 to 10
put every instance of <max digits> in "Issue #429 was resolved on 15-Jun-2018" after position 10 --> [15,2018]

例:

set KingQuotes to {{
Darkness cannot drive out darkness; only light can do that. Hate cannot drive out hate; only love can do that.
The ultimate measure of a man is not where he stands in moments of comfort and convenience, but where he stands at times of challenge and controversy.
Faith is taking the first step even when you don't see the whole staircase.
Our lives begin to end the day we become silent about things that matter.
Injustice anywhere is a threat to justice everywhere.
I look to a day when people will not be judged by the color of their skin, but by the content of their character.
I have decided to stick with love. Hate is too great a burden to bear.
The time is always right to do what is right.
Life's most persistent and urgent question is, "What are you doing for others?"
We must learn to live together as brothers or perish together as fools.
}}

}}
set LWords to <word beginning with "L", chars, word break>
put every occurrence of LWords in KingQuotes

--> [light,love,lives,look,love,Life,learn,live]
set JWords to <"J" at start of a word, chars, end of word>
put every match of JWords in KingQuotes

関連項目: