データ発見・分類のソリューションは、機密データを特定するために正規表現(RegExes、REs、または RegEx パターンとも呼ばれます)に大きく依存しています。では、RegExes とは何で、機密データの発見にどう活用できるのでしょうか。早速見ていきましょう。
厳選した関連コンテンツ:
正規表現(Regular expressions)は、小さいながらも非常に特化したプログラミング言語で、基本的には「ステロイド入りのワイルドカード」です。この小さな言語を使って、マッチさせたい文字列を定義するルールを指定します。たとえば、メールアドレス、PII、PHI、またはクレジットカード番号に一致する RegEx を定義できます。
Regex の構成要素
RegEx にはリテラルとメタキャラクタを含めることができます。
リテラル
メタキャラクタとして予約されているものを除き、どんな1文字でもそれ自体がすでに正規表現です。たとえば、 www は www.Netwrix.com に一致しますが wwz は一致しません。正規表現は大文字と小文字を区別するため、 www は WWW や wWw には一致しません。
メタキャラクター
以下の単一文字はリテラルとして解釈されず、代わりに特別な意味を持ちます:
- . ^ $ * + ? { } [ ] | ( )
以下の表は、これらのメタキャラクターそれぞれがどのように機能するかを説明しています。
Type | Meta-characters | Description | Examples |
|---|---|---|---|
|
The dot |
. |
The period means any character. |
net.rix will match both www.netwrix.com and www.netfrix.com. |
|
Character class |
[] |
Matches for anything inside the square brackets. |
You can list characters individually; for instance, net[wrx] will match netw , netr and netx but not netz. |
|
Anchors |
^ |
Used to match characters at the beginning of a string |
^https will match https://netwrix.com but not www.netwrix.com or http://netwrix.com |
|
$ |
Used to match characters at the end of a string |
com$ will match www.netwrix.com or telecom but not computer. |
|
|
Iteration / quantifiers |
? |
Matches the preceding element zero or one time (it will always match if the character was not found). It is great for finding optional characters. |
colou?r will match both color and colour. |
|
|
* |
Matches the preceding element zero or more times instead of zero or once. It is great for finding optional series of characters. |
ne*t will match nt (zero e characters), net (one e ), neeet (three e characters), and so forth. |
|
+ |
Matches the preceding element one or more times. |
ne+t will match net and neeet but not nt. |
|
|
| |
The choice operator matches either the expression before or the expression after the operator. |
net|wrix will match net and wrix. |
|
|
{} |
{x} matches if the element that precedes it is found exactly x times. |
n{3} will match nnn , nnnn and nnnd (because they all include n three times in a row), but it will not match nnw. |
|
|
Blocking and capturing |
() |
Defines a subexpression that can be recalled later using shorthand: The first subexpression in parentheses can be recalled by \1, the second can be recalled by \2 and so on. |
Gr(a|e)y will match Gray or Grey. |
|
Escape sequence |
\ |
The metacharacter that follows the slash will be used as a literal. |
www\.netwrix\.com will match www.netwrix.com but not www,netwrix,com. |
|
Special metacharacters |
\s |
Matches any whitespace character (a space, a tab, a line break or a form feed). |
Netwrix\sAuditor will match Netwrix Auditor, and Netwrix(tab)Auditor, but not Netwrix<5 spaces> Auditor or NetwrixAuditor. |
|
|
\S |
Matches any non-whitespace character. |
\Snetwrix will match Xnetwrix and 1netwrix. |
|
\w |
Matches any alphanumeric character. |
\w\w\w will match net, dfw and Netwrix. |
|
|
\W |
Matches any non-alphanumeric character. |
netwrix\W will match netwrix! and netwrix?. |
|
|
|
\d |
Matches any decimal digit. |
Netwrix\d\d will match Netwrix80 and Netwrix90. |
|
\D |
Matches any non-digit character. |
Netwrix\D will match Netwrix) and Netwrix-. |
|
|
\a |
Matches any single alphabetic character, either capital or lowercase. |
net\arix will match netWrix, netfrix and netarix. |
|
|
|
\b |
Defines a word boundary. |
\brix will match rix and rixon but not netwrix. |
|
|
\B |
Defines a non-word boundary |
\Brix will match Netwrix and trix but not rixon. |
メタキャラクターの組み合わせ
これで、ほとんどすべてのメタキャラクターを理解できたので、それらを組み合わせる準備ができました。
例:ナンバープレートの番号を探す
たとえば、次の形式のライセンス番号(番号)を見つける必要があるとします:aaa-nnnn — 最初の3文字は英数字で、最後の4文字は数字でなければなりません。ハイフンは任意の文字に置き換えることも、まったくない状態でも構いません。
このときの正規表現(RegEx)は次のとおりです:
- b[0-9A-Z]{3}([^ 0-9A-Z]|s)?[0-9]{4}b
この RegEx を分解してみましょう:
- b は単語境界を必要とするため、マッチした文字列はより長い文字列の一部にはなれません。
- [0-9A-Z]{3} は、最初の3文字が英数字でなければならないことを意味します。
- ([^ 0-9A-Z]|s)? は、文字列の次の部分がデリミタ(英数字以外の文字または空白文字)であるか、またはまったくないことを意味します。
- [0-9]{4} は、文字列の次の部分が4桁である必要があることを意味します。
- b は別の単語の境界を指定します。
この RegEx は次のライセンス番号に一致します:NT5-6345, GH3 9452, XS83289
ただし、次のライセンス番号には一致しません:ZNT49371, HG3-29347, nt4-9371
例:社会保障番号(Social Security number)を探す
もう1つの良い例は、米国の社会保障番号(SSN)で、常に次の形式を取ります:nnn-nn-nnnn。
最も簡単な RegEx は次のとおりです:
- [0-9]{3}-[0-9]{2}-[0-9]{4}
しかし、これは誤検知を生成します。なぜなら、この形式になっているすべての数字が正当な SSN とは限らないからです。さらに、ハイフンなしで書かれているものなど、一部の実際の SSN を見逃してしまいます。より正確な結果を得るには、もっと複雑なものを作るべきです。私たちは次のことを知っています:
- いずれの数字グループもすべてゼロであることはできません。
- 最初のブロックは 666 または 900-999 であってはなりません。
- SSN は、ハイフンの代わりに空白文字で書くこともでき、区切り記号を一切使わずにそのまま書くこともできます。
- 最初のブロックが 7 で始まる場合、それに続いて 0 から 6 の間の数字が入り、その後は任意の3桁目(第三の数字)になります。
したがって、高度な RegEx は次のようになります:
- b(?!000|666|9d{2})([0-8]d{2}|7([0-6]d))([-]?|s{1})(?!00)dd2(?!0000)d{4}b
以前と同様に b は先頭と末尾で単語境界を指定します。中間にある各数字ブロックを、もう少し詳しく見ていきましょう。
最初のブロック
- (?!000|666|9d{2}) は、数字が次で始まってはいけないことを指定する否定の先読み(negative look-ahead)です。000, 666, または 9 の後に任意の2桁が続く場合。
- ([0-8]d{2} は、文字列が 0 と 8 の間の数字で始まり、その後にさらに2桁の数字 (0-9) が続く必要があることを指定します。
- |7[0-6]d)) は、その値が 7 で始まる場合、次の1桁は 0 と 6 の間で、その後ろには任意の1桁が続くことを意味します。
- ([-]?|s{1}) は、3桁の数字の後に、ハイフン、空白文字、または何もない状態のいずれかが続き、第1ブロックの終わりであることを示すことを指定します。
2つ目のブロック
- (?!00) は、別の否定的な先読みであり、2つ目のブロックに 00 が存在してはならないことを指定します。
- dd は、2つ目のブロックに任意の2桁の数字が必要であることを指定します。
- 2 は、2つ目のキャプチャグループと同じテキストに一致します。それは ([-]?|s{1}), なので、2つ目のブロックがハイフン、空白文字、または追加の文字がまったくない状態で終わってよいことを指定します。
3つ目のブロック
- (?!0000) は、3つ目のブロックに 0 が4つ入っていてはならないことを指定する別の否定的先読み(negative look-ahead)です。
- d{4} は、3つ目のSSNブロックに任意の4桁の数字が必要であることを意味します。
よく使われる RegEx の例
To find | Use this RegEx | Example of match |
|---|---|---|
|
Email addresses |
^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$ |
T.Simpson@netwrix.com |
|
U.S. Social Security numbers |
\b(?!000|666|9\d{2})([0-8]\d{2}|7([0-6]\d))([-]?|\s{1})(?!00)\d\d\2(?!0000)\d{4}\b |
513-84-7329 |
|
IPV4 addresses |
^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$ |
192.168.1.1 |
|
Dates in MM/DD/YYYY format |
^([1][12]|[0]?[1-9])[\/-]([3][01]|[12]\d|[0]?[1-9])[\/-](\d{4}|\d{2})$ |
05/05/2018 |
|
MasterCard numbers |
^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$ |
5258704108753590 |
|
Visa card numbers |
\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[- |
4563-7568-5698-4587 |
|
American Express card numbers |
^3[47][0-9]{13}$ |
34583547858682157 |
|
U.S. ZIP codes |
^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$ |
97589 |
|
File paths |
\\[^\\]+$ |
\\fs1\shared |
|
URLs |
(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+ |
www.netwrix.com |
役立つ Regex のWebリソース
- https://regexr.com と https://regex101.com は、構文のハイライトとツールチップによって RegEx を確認するのに役立ちます。
- https://regexcrossword.com は、手がかりが正規表現を使って定義されるクロスワードパズルゲームです。
- https://www.regular-expressions.info 正規表現に関する情報が充実した素晴らしいサイトです。さらに、Notepad++ には正規表現を扱う作業中に役立つ RegEx ヘルパー拡張機能があります。
共有する
もっと詳しく
著者について
Jeff Melnick
システムエンジニアリング ディレクター
Jeff は Netwrix における Global Solutions Engineering の元ディレクターです。彼は長年にわたり Netwrix のブログ執筆者であり、講演者、プレゼンターとしても活躍しています。Netwrix のブログでは、Jeff がシステム管理の体験を大きく改善できるライフハックや、役立つヒント、ちょっとしたコツを共有しています。