PHPの正規表現
正規表現とは何ですか?
正規表現は、検索パターンを形成する一連の文字です。テキスト内のデータを検索する場合、この検索パターンを使用して、検索対象を説明できます。
正規表現は、単一の文字でも、より複雑なパターンでもかまいません。
正規表現を使用して、すべてのタイプのテキスト検索およびテキスト置換操作を実行できます。
構文
PHPでは、正規表現は、区切り文字、パターン、およびオプションの修飾子で構成される文字列です。
$exp = "/w3schools/i";
上記の例で/
は、は区切り文字、w3schoolsは検索されるパターンであり、検索で大文字と小文字を区別しないようにするi
修飾子です。
区切り文字は、文字、数字、円記号、またはスペース以外の任意の文字にすることができます。最も一般的な区切り文字はスラッシュ(/)ですが、パターンにスラッシュが含まれている場合は、#や〜などの他の区切り文字を選択すると便利です。
正規表現関数
PHPには、正規表現を使用できるさまざまな関数が用意されています。preg_match()
、preg_match_all()
および関数は、最も一般的に使用される関数の
preg_replace()
一部です。
Function | Description |
---|---|
preg_match() | Returns 1 if the pattern was found in the string and 0 if not |
preg_match_all() | Returns the number of times the pattern was found in the string, which may also be 0 |
preg_replace() | Returns a new string where matched patterns have been replaced with another string |
preg_match()の使用
このpreg_match()
関数は、文字列にパターンの一致が含まれているかどうかを示します。
例
正規表現を使用して、文字列内の「w3schools」で大文字と小文字を区別しない検索を実行します。
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
preg_match_all()の使用
このpreg_match_all()
関数は、文字列内のパターンに対して見つかった一致の数を示します。
例
正規表現を使用して、文字列内の「ain」の出現回数を大文字と小文字を区別せずにカウントします。
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>
preg_replace()の使用
このpreg_replace()
関数は、文字列内のパターンのすべての一致を別の文字列に置き換えます。
例
大文字と小文字を区別しない正規表現を使用して、文字列内でMicrosoftをW3Schoolsに置き換えます。
<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo
preg_replace($pattern, "W3Schools", $str); // Outputs "Visit
W3Schools!"
?>
正規表現修飾子
修飾子は、検索の実行方法を変更できます。
Modifier | Description |
---|---|
i | Performs a case-insensitive search |
m | Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line) |
u | Enables correct matching of UTF-8 encoded patterns |
正規表現パターン
角かっこは、文字の範囲を見つけるために使用されます。
Expression | Description |
---|---|
[abc] | Find one character from the options between the brackets |
[^abc] | Find any character NOT between the brackets |
[0-9] | Find one character from the range 0 to 9 |
メタ文字
メタ文字は、特別な意味を持つ文字です。
Metacharacter | Description |
---|---|
| | Find a match for any one of the patterns separated by | as in: cat|dog|fish |
. | Find just one instance of any character |
^ | Finds a match as the beginning of a string as in: ^Hello |
$ | Finds a match at the end of the string as in: World$ |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
量指定子
数量詞は数量を定義します。
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{x} | Matches any string that contains a sequence of X n's |
n{x,y} | Matches any string that contains a sequence of X to Y n's |
n{x,} | Matches any string that contains a sequence of at least X n's |
注:式で特殊文字の1つを検索する必要がある場合は、円記号(\)を使用してそれらをエスケープできます。たとえば、1つ以上の疑問符を検索するには、次の式を使用できます。$ pattern = '/ \?+ /';
グループ化
括弧( )
を使用して、パターン全体に数量詞を適用できます。また、一致として使用するパターンの部分を選択するために使用することもできます。
例
グループ化を使用して、 baの後にnaの2つのインスタンスを検索して、「バナナ」という単語を検索します。
<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
完全な正規表現リファレンス
完全なリファレンスについては、完全なPHP正規表現リファレンスを参照してください。
このリファレンスには、すべての正規表現関数の説明と例が含まれています。