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 = 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
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
16 進数に一致
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"
入力引数
pat
— 入力パターン
pattern 配列 | string 配列 | 文字ベクトル | 文字ベクトルの cell 配列
入力パターン。pattern
、string 配列、文字ベクトルまたは文字ベクトルの cell 配列として指定します。
データ型: char
| string
| pattern
| cell
mask
— マスクされたパターン名
string 配列 | 文字ベクトル | 文字ベクトルの cell 配列
マスクされたパターン名。string 配列、文字ベクトル、または文字ベクトルの cell 配列として指定します。
データ型: char
| string
| cell
出力引数
バージョン履歴
R2020b で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)