定義
構文
$regexMatch演算子の構文は次のとおりです。
{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }フィールド | 説明 |
|---|---|
適用する正規表現パターン。 または正規表現パターン のいずれかに解決される任意の有効な 式 を指定できます。string
あるいは、オプションフィールドを使用して正規表現オプションを指定することもできます。
| |
任意。次の
|
戻り値
この演算子は、次のようにブール値を返します。
true: 一致するものが存在する場合false: 一致するものが存在しない場合
動作
PCRE ライブラリ
バージョン 6.1 以降、MongoDB は、正規式によるパターン一致を実装するためにPerl互換正規表現 (PCRE2) ライブラリを使用します。PCRE2 の詳細については、PCRE ドキュメント を参照してください。
$regexMatch と照合
$regexMatch の string マッチングは、常に大文字と小文字を区別し、発音区別符号も区別します。 $regexMatch は、コレクション、db.collection.aggregate()、およびインデックスに指定されている照合(使用されている場合)を無視します。
例、照合強度が 1 のコレクションを作成します。つまり、照合では基本文字のみが比較され、大文字と小文字や発音区別符号などの違いは無視されます。
db.createCollection( "restaurants", { collation: { locale: "fr", strength: 1 } } )次のドキュメントを挿入します。
db.restaurants.insertMany( [ { _id: 1, category: "café", status: "Open" }, { _id: 2, category: "cafe", status: "open" }, { _id: 3, category: "cafE", status: "open" }] )次の例では、 コレクションの照合を使用して、大文字と小文字を区別せず、発音区別符号も区別しない一致を実行します。
db.restaurants.aggregate( [ { $match: { category: "cafe" } } ] )[ { _id: 1, category: 'café', status: 'Open' }, { _id: 2, category: 'cafe', status: 'open' }, { _id: 3, category: 'cafE', status: 'open' }]ただし、$regexMatch は照合を無視します。次の正規式パターン マッチングの例では、大文字と小文字が区別され、発音区別符号も区別されます。
db.restaurants.aggregate( [ { $addFields: { resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } } } }] )
db.restaurants.aggregate( [ { $addFields: { resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } } } }], { collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch)どちらの操作も以下を返します。
{ "_id" : 1, "category" : "café", "resultObject" : null }{ "_id" : 2, "category" : "cafe", "resultObject" : { "match" : "cafe", "idx" : 0, "captures" : [ ] } }{ "_id" : 3, "category" : "cafE", "resultObject" : null }クエリは照合を無視するため、category string(大文字と小文字、アクセント記号を含む)の完全一致が必要になります。つまり、ドキュメント_id: 2 のみが一致します。
大文字と小文字を区別しない正規表現パターン マッチングを実行するには、代わりにiオプションを使用します。 例については、 iオプションを参照してください。
例
$regexMatch とそのオプション
この例で説明されている$regexMatch演算子の動作を示すには、次のドキュメントを含むサンプル コレクションproductsを作成します。
db.products.insertMany([ { _id: 1, description: "Single LINE description." }, { _id: 2, description: "First lines\nsecond line" }, { _id: 3, description: "Many spaces before line" }, { _id: 4, description: "Multiple\nline descriptions" }, { _id: 5, description: "anchors, links and hyperlinks" }, { _id: 6, description: "métier work vocation" }])デフォルトでは、 $regexMatchは大文字と小文字を区別する一致を実行します。 たとえば、次の集計ではdescriptionフィールドで大文字と小文字を区別する$regexMatchが実行されます。 正規表現パターン/line/ではグループは指定されていません。
db.products.aggregate([ { $addFields: { result: { $regexMatch: { input: "$description", regex: /line/ } } } }])この操作では、以下を返します。
{ "_id" : 1, "description" : "Single LINE description.", "result" : false }{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }{ "_id" : 3, "description" : "Many spaces before line", "result" : true }{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }{ "_id" : 6, "description" : "métier work vocation", "result" : false }次の正規表現パターン/lin(e|k)/は、パターン内にグループ化(e|k)を指定します。
db.products.aggregate([ { $addFields: { result: { $regexMatch: { input: "$description", regex: /lin(e|k)/ } } } }])この操作では、以下を返します。
{ "_id" : 1, "description" : "Single LINE description.", "result" : false }{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }{ "_id" : 3, "description" : "Many spaces before line", "result" : true }{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : true }{ "_id" : 6, "description" : "métier work vocation", "result" : false }i オプション
注意
regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。
大文字と 小文字 を区別しないパターン一致を実行するには、 正規表現 フィールドの一部または オプション フィールドに i オプションを含めます。
// Specify i as part of the regex field{ $regexMatch: { input: "$description", regex: /line/i } }
// Specify i in the options field{ $regexMatch: { input: "$description", regex: /line/, options: "i" } }{ $regexMatch: { input: "$description", regex: "line", options: "i" } }For example, the following aggregation performs a case-insensitive $regexMatch on the description field. 正規表現パターン/line/ではグループは指定されていません。
db.products.aggregate([ { $addFields: { result: { $regexMatch: { input: "$description", regex: /line/i } } } }])この操作により、次のドキュメントが返されます。
{ "_id" : 1, "description" : "Single LINE description.", "result" : true }{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }{ "_id" : 3, "description" : "Many spaces before line", "result" : true }{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }{ "_id" : 6, "description" : "métier work vocation", "result" : false }m オプション
注意
regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。
指定されたアンカー(例: ^、$ )の各行には、string 正規表現 フィールドの一部または オプション フィールドに m オプションを含め 。
// Specify m as part of the regex field{ $regexMatch: { input: "$description", regex: /line/m } }
// Specify m in the options field{ $regexMatch: { input: "$description", regex: /line/, options: "m" } }{ $regexMatch: { input: "$description", regex: "line", options: "m" } }次の例には、複数行の文字列に対して文字i mまたは で始まる行を検索するために、s オプションとS オプションの両方が含まれています。
db.products.aggregate([ { $addFields: { result: { $regexMatch: { input: "$description", regex: /^s/im } } } }])この操作では、以下を返します。
{ "_id" : 1, "description" : "Single LINE description.", "result" : true }{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }{ "_id" : 3, "description" : "Many spaces before line", "result" : false }{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : false }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }{ "_id" : 6, "description" : "métier work vocation", "result" : false }x オプション
注意
regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。
パターン内のエスケープされていない空白文字とコメント(エスケープされていないハッシュ「# 」文字と次の改行文字によって示される)を無視するには、 オプション フィールドに s オプションを含めます。
// Specify x in the options field{ $regexMatch: { input: "$description", regex: /line/, options: "x" } }{ $regexMatch: { input: "$description", regex: "line", options: "x" } }次の例には、エスケープされていない空白とコメントをスキップするためのxオプションが含まれています。
db.products.aggregate([ { $addFields: { returns: { $regexMatch: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }])この操作では、以下を返します。
{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : true }{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : true }{ "_id" : 6, "description" : "métier work vocation", "returns" : false }s オプション
注意
regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。
ドット記号( .)が改行文字 を含む すべての文字と一致するようにするには、 オプション フィールドに s オプションを含めます。
// Specify s in the options field{ $regexMatch: { input: "$description", regex: /m.*line/, options: "s" } }{ $regexMatch: { input: "$description", regex: "m.*line", options: "s" } }次の例には、ドット文字()が改行を 含む すべての文字と一致するようにするためのsオプションと、大文字と小文字を区別しない一致を実行するためのiオプションが含まれています。
db.products.aggregate([ { $addFields: { returns: { $regexMatch: { input: "$description", regex:/m.*line/, options: "si" } } } }])この操作では、以下を返します。
{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : false }{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : false }{ "_id" : 6, "description" : "métier work vocation", "returns" : false }$regexMatch を使用してメールアドレスを確認する
次のドキュメントを含む feedback のサンプル コレクションを作成します。
db.feedback.insertMany([ { "_id" : 1, comment: "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com" }, { "_id" : 2, comment: "I wanted to concatenate a string" }, { "_id" : 3, comment: "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com" }, { "_id" : 4, comment: "It's just me. I'm testing. fred@MongoDB.com" }])次の集計では、 $regexMatchを使用して、 commentフィールドに@mongodb.comのメールアドレスが含まれているかどうかを確認し、フィードバックをEmployeeまたはExternalに分類します。
db.feedback.aggregate( [ { $addFields: { "category": { $cond: { if: { $regexMatch: { input: "$comment", regex: /[a-z0-9_.+-]+@mongodb.com/i } }, then: "Employee", else: "External" } } } },この操作により、次のドキュメントが返されます。
{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com", "category" : "External" }{ "_id" : 2, "comment" : "I wanted to concatenate a string", "category" : "External" }{ "_id" : 3, "comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com", "category" : "Employee" }{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "category" : "Employee" }