PHPチュートリアル

PHPホーム PHPイントロ PHPインストール PHP構文 PHPコメント PHP変数 PHPエコー/印刷 PHPデータ型 PHP文字列 PHP番号 PHP数学 PHP定数 PHP演算子 PHP If ... Else ... Elseif PHPスイッチ PHPループ PHP関数 PHP配列 PHPスーパーグローバル PHP正規表現

PHPフォーム

PHPフォーム処理 PHPフォームの検証 PHPフォームが必要 PHPフォームのURL / Eメール PHPフォームの完了

PHP Advanced

PHPの日付と時刻 PHPインクルード PHPファイルの処理 PHPファイルのオープン/読み取り PHPファイルの作成/書き込み PHPファイルのアップロード PHPクッキー PHPセッション PHPフィルター PHPフィルターアドバンスト PHPコールバック関数 PHP JSON PHPの例外

PHPOOP _

PHPOOPとは PHPクラス/オブジェクト PHPコンストラクター PHPデストラクタ PHPアクセス修飾子 PHPの継承 PHP定数 PHP抽象クラス PHPインターフェース PHPの特性 PHP静的メソッド PHPの静的プロパティ PHP名前空間 PHPIterables

MySQLデータベース

MySQLデータベース MySQLコネクト MySQL Create DB MySQLテーブルの作成 MySQLの挿入データ MySQLは最後のIDを取得します MySQL Insert Multiple MySQLを準備しました MySQL Select Data MySQL Where MySQL Order By MySQLデータの削除 MySQLアップデートデータ MySQL制限データ

PHP XML

PHPXMLパーサー PHPSimpleXMLパーサー PHPSimpleXML-取得 PHP XMLExpat PHP XML DOM

PHP -AJAX

AJAXイントロ AJAX PHP AJAXデータベース AJAX XML AJAXライブ検索 AJAXポール

PHPの

PHPの例 PHPコンパイラ PHPクイズ PHP演習 PHP証明書

PHPリファレンス

PHPの概要 PHP配列 PHPカレンダー PHPの日付 PHPディレクトリ PHPエラー PHP例外 PHPファイルシステム PHPフィルター PHP FTP PHP JSON PHPキーワード PHP Libxml PHPメール PHP数学 PHPその他 PHP MySQLi PHPネットワーク PHP出力制御 PHP正規表現 PHP SimpleXML PHPストリーム PHP文字列 PHP変数の処理 PHPXMLパーサー PHP Zip PHPタイムゾーン

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正規表現リファレンスを参照してください。

このリファレンスには、すべての正規表現関数の説明と例が含まれています。