maskedPattern
指定した表示名をもつパターン
説明
例
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
Show all details
いくつかの演算式を含む string を作成した後、テキストからパターンを抽出します。
txt = "What is the answer to 1 + 1? Oh, I know! 1 + 1 = 2!";
arithmetic = extract(txt,shortExpressionPat)arithmetic = 2×1 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
Show all details
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
Show all details
hexDigit は、16 進数の個別の数字に一致します。リテラル テキスト "0x" の後に、1 つ以上の hexDigit が出現するものに一致するパターンを作成して、16 進数全体に一致させます。
hexNumberPattern = "0x" + asManyOfPattern(hexDigit, 1)hexNumberPattern = pattern
Matching:
"0x" + asManyOfPattern(hexDigit,1)
Show all details
このパターンを使用して、string から 16 進数を抽出します。
hexNumbers = extract("The answer is 0x2A", hexNumberPattern)hexNumbers = "0x2A"
入力引数
拡張機能
スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。
バージョン履歴
R2020b で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)