Main Content

maskedPattern

指定した表示名をもつパターン

R2020b 以降

説明

newpat = maskedPattern(pat) は、newpat のパターン式を表示するときに、pat の入力名を使用するパターンを作成します。maskedPattern を使用すると、式の詳細の一部を非表示にすることで、長い複雑なパターン式を簡略化できます。

newpat = maskedPattern(pat,mask) は、newpat のパターン式を表示するときの表示名 mask を指定します。

すべて折りたたむ

maskedPattern を使用して、複雑なパターン式の代わりに変数を表示します。

数字と算術演算子で構成されるシンプルな演算式に一致するパターンを作成します。

mathSymbols = asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)
mathSymbols = pattern
  Matching:

    asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)

mathSymbols を使用して、文字と文字の間に空白を含む演算式に一致するパターンを作成します。

longExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols
longExpressionPat = pattern
  Matching:

    asManyOfPattern(asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1) + whitespacePattern) + asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)

表示されるパターン式は長く、可読性が良くありません。maskedPattern を使用して、パターン式の代わりに、変数名 mathSymbols を表示します。

mathSymbols = maskedPattern(mathSymbols);
shortExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols
shortExpressionPat = pattern
  Matching:

    asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols

  Use details to show more information

いくつかの演算式を含む string を作成した後、テキストからパターンを抽出します。

txt = "What is the answer to 1 + 1? Oh, I know! 1 + 1 = 2!";
arithmetic = extract(txt,shortExpressionPat)
arithmetic = 2x1 string
    "1 + 1"
    "1 + 1 = 2"

maskedPattern を使用して、複雑なパターン式に対して、指定した名前を表示します。

次の 2 つのパターンを作成します。1 つは、先頭と末尾が文字 D である単語に一致させます。もう 1 つは、先頭と末尾が文字 R である単語に一致させます。

dWordsPat = letterBoundary + caseInsensitivePattern("d" + lettersPattern + "d") + letterBoundary;
rWordsPat = letterBoundary + caseInsensitivePattern("r" + lettersPattern + "r") + letterBoundary;

maskedPattern を使用して、パターン式の代わりに、指定した名前を表示します。先頭と末尾が D である単語を検索し、続けて先頭と末尾が R である単語を検索する、マスクされたパターンを使用して 1 つのパターンを作成します。

dWordsPat = maskedPattern(dWordsPat,"Words that start and end with D");
rWordsPat = maskedPattern(rWordsPat,"Words that start and end with R");
dAndRWordsPat = dWordsPat + whitespacePattern + rWordsPat
dAndRWordsPat = pattern
  Matching:

    Words that start and end with D + whitespacePattern + Words that start and end with R

  Use details to show more information

string を作成した後、テキストからパターンを抽出します。

txt = "Dad, look at the divided river!";
words = extract(txt,dAndRWordsPat)
words = 
"divided river"

カスタムのパターン関数を作成し、maskedPattern を使用して詳細を非表示にします。

入力パターン pat を取り、pat の 1 つ以上の連続するインスタンスに一致するパターン newPat を作成する関数 atLeastOneOfPattern を作成します。maskedPattern を使用して、表示時に、パターンの詳細が表示されないようにします。

function newPat = atLeastOneOfPattern(pat)
arguments
    pat pattern
end

newPat = asManyOfPattern(pat,1);
newPat = maskedPattern(newPat,compose("atLeastOneOfPattern(%s)",pat));

end

入力を "a" として atLeastOneOfPattern を呼び出し、newPat を表示します。

newPat = atLeastOneOfPattern("a")
newPat = 

  pattern

  Matching:

    atLeastOneOfPattern("a")

  Show all details

characterListPattern を使用して、0 ~ 9 の数字と a ~ f の文字に一致するパターン hexDigit を作成します。characterListPattern は大文字と小文字を区別するため、caseInsensitivePattern を使用して hexDigit が大文字と小文字の区別なく一致するようにします。

hexDigit = characterListPattern('0','9') | characterListPattern('a','f');
hexDigit = caseInsensitivePattern(hexDigit);
hexDigit = maskedPattern(hexDigit)
hexDigit = pattern
  Matching:

    hexDigit

  Use details to show more information

hexDigit は、16 進数の個別の数字に一致します。リテラル テキスト "0x" の後に、1 つ以上の hexDigit が出現するものに一致するパターンを作成して、16 進数全体に一致させます。

hexNumberPattern = "0x" + asManyOfPattern(hexDigit, 1)
hexNumberPattern = pattern
  Matching:

    "0x" + asManyOfPattern(hexDigit,1)

  Use details to show more information

このパターンを使用して、string から 16 進数を抽出します。

hexNumbers = extract("The answer is 0x2A", hexNumberPattern)
hexNumbers = 
"0x2A"

入力引数

すべて折りたたむ

入力パターン。pattern、string 配列、文字ベクトルまたは文字ベクトルの cell 配列として指定します。

データ型: char | string | pattern | cell

マスクされたパターン名。string スカラー、文字ベクトル、または文字ベクトルの cell 配列として指定します。

データ型: char | string | cell

出力引数

すべて折りたたむ

出力パターン。patternまたは pattern オブジェクトの配列として返されます。

拡張機能

スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。

バージョン履歴

R2020b で導入