ホームページ  >  記事  >  バックエンド開発  >  PHP 正規表現

PHP 正規表現

PHPz
PHPzオリジナル
2024-08-29 13:01:52400ブラウズ

正規表現は、単一行で生成されるパターン マッチング アルゴリズムとして定義できます。これらは、検証チェックやテンプレート認識の場合に影響を与えます。メタ文字を使用すると、ユーザーは複雑なパターンを処理できるようになります。したがって、PHP での正規表現のサポートは、PHP プログラミングのコード品質の向上に役立ちます。正規表現は、特定の対象文字列に対するパターン マッチング機能を提供するために使用される、一連の文字の一般的なパターンまたはシーケンスです。 RegExp または RegEx とも呼ばれます。また、テキスト文字列の解析に使用されるパターン表記ベースの小規模なプログラミング言語としても考慮されています。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP 関数の正規表現の 2 セット

以下の例では 2 セットの正規表現がサポートされています:

  • POSIX 正規表現
  • PERL スタイルの正規表現

1. POSIX 正規表現

これは、単一文字のいずれかが入力文字列と一致する必要がある文字セットとして定義されます。これらの式は [].

内で定義されます。

例:

  • [0-9]: これは、0 から 9 までの任意の 10 進文字列をフィルターするように設計されています。
  • [a-Z]: これは、小文字の「a」から大文字の「Z」までの任意の文字をフィルタリングするように設計されています。

フィルターをより具体的にするために、正規表現と特殊文字を含む標準構文が開発されており、これは量指定子として知られています。また、頻度に関する情報、つまり括弧で囲まれた文字または文字のグループの出現数やインスタンスの数、および量も提供します。

さまざまな量指定子の説明を示す表:

Quantifier Description
S+ Filters out a string having at least one ‘s’.
S* Filters out a string having zero or more ‘s’.
S? Filters out a string having zero or one ‘s’.
S{N} Filters out a string having a sequence of N ‘s’.
S$ Filters out a string having ‘s’ at the end.
^S Filters out a string having ‘s’ at the beginning.
数量子

説明

S+ 少なくとも 1 つの「s」を含む文字列を除外します。 S* 0 個以上の「」を含む文字列をフィルタリングして除外します。 S? 0 個または 1 個の「」を含む文字列をフィルタリングして除外します。 S{N} N 個の「」が連続する文字列をフィルタリングして除外します。 S$ 末尾に「s」を含む文字列を除外します。 ^S 先頭に「s」を含む文字列をフィルタリングします。 テーブル> PHP は、事前定義された文字範囲/クラスに関するマッチング機能もサポートしています。
predefined character class Description
[[:space:]] Filters out a string having a space.
[[:alpha:]] Filters out a string having alphabetic characters a-A through z-Z.
[[:digit:]] Filters out a string having numerical 0 to 9.
[[:alnum:]] Filters out a string having alphanumeric characters a-A through z-Z and numerical 0 to 9.
例: 事前定義された文字クラス 説明 [[:space:]] スペースを含む文字列を除外します。 [[:alpha:]] アルファベットの a-A ~ z-Z を含む文字列をフィルタリングして除外します。 [[:digit:]] 0 ~ 9 の数値を含む文字列をフィルタリングして除外します。 [[:alnum:]] 英数字 a-A ~ z-Z と数字 0 ~ 9 を含む文字列をフィルタリングします。 テーブル>

POSIX 正規表現の場合、PHP には POSIX スタイルの正規表現を使用してさまざまな操作を実行するためのさまざまな機能が組み込まれています。

関数は次の表に示すように説明されています。

POSIX Regex function Description
ereg() Used to search a string specified by or by pattern and to return true if the matching is found.
ereg_replace() Used to search a string specified by or by pattern and replace with replacement if the matching is found.
eregi() Used to perform non-case sensitive search for a string specified by or by pattern and to return true if the matching is found.
eregi_replace() Used to perform non-case sensitive for a string specified by or by pattern and replace with replacement if the matching is found.
split() Used to divide the string into separate elements based on boundaries that matches the searching pattern.
spliti() Used to perform non-case sensitive for the string to divide it into separate elements based on boundaries that matches the searching pattern.
sql_regcase() A utility function that convert each character from the input value into a bracketed expression making two characters.

2. PERL スタイルの正規表現

このタイプの正規表現パターンは POSIX 正規表現に似ていますが、メタ文字と識別子を使用して作成されます。この正規表現の構文は POSIX スタイルと互換性があります。

a.メタ文字: 特定の意味を表すバックスラッシュが前にあるアルファベット文字は、メタ文字として知られています。

PHP スクリプトではさまざまなメタ文字がサポートされており、以下で説明するように Perl タイプの Regex として使用されます。

Meta character Description
. Single character
d A digit character
D Non-digit character
s white space character e.g. SPACE, NEW LINE, TAB
S Non- white space character
w A word character
W Non-word character
[aeiou] Filters the matched character out of the given set
[^aeiou] Filters the unmatched character out of the given set
(set1|set2|set3) Filters the matched element that matches to any of the given alternatives
メタ文字

説明 . 単一文字 d 数字の文字 D 数字以外の文字 s 空白文字 例:スペース、改行、タブ S 空白以外の文字 w 単語の文字 幅 単語以外の文字 [アイオウ] 指定されたセットから一致した文字をフィルタリングします [^aeiou] 指定されたセットから一致しない文字をフィルタリングします (セット1|セット2|セット3) 指定された選択肢のいずれかに一致する要素をフィルタリングします テーブル>

b.修飾子:

これらの要素を使用すると、ユーザーは正規表現をさらに柔軟に操作できるようになります。
Modifier Description
g Finds matchings globally.
cg Enable continue global search even after matching fails.
i Instructs to perform case insensitive search.
s Use the character ‘.’ to search for new line character.
m In case of input string containing new line or carriage return character, ‘^’ and ‘$’ are used to match for new line boundary.
x Permits to use white space to improve the clarity of the expression.
o Restrict the evaluation of the expression to occur only once.
さまざまな修飾子とその機能を以下の表に示します。 修飾子 説明 g 一致するものをグローバルに検索します。 cg 一致が失敗した後でもグローバル検索を続行できるようにします。 私 大文字と小文字を区別しない検索を実行するように指示します。 s 改行文字を検索するには、文字「.」を使用します。 m 入力文字列に改行または復帰文字が含まれる場合、改行境界の一致に「^」と「$」が使用されます。 x 式をわかりやすくするために空白を使用することができます。 o 式の評価が 1 回だけ行われるように制限します。 テーブル>

POSIX 正規表現関数と同様に、PHP も PERL スタイルの正規表現と互換性のある特定の関数をいくつか提供します。

主要な機能のいくつかについては以下で説明します。

PERL style regexpcompitable function Description
preg_match() Return the first occurrence of the matching pattern.
preg_match_all() Return all occurrences of the matching pattern.
preg_split() Splits the string input into several elements based on the given regexp pattern as input.
Preg_quote() Used to quote the characters of the regex.
preg_grep() Used to find all the matching elements from array input.
preg_replace() Used to find matching element and replace it with the given replacement.

PHP 正規表現の例

以下の例はアプリケーションを示しています

このコード スニペットは、入力文字列をスキャンし、指定された正規表現を境界として定義することによって、指定された入力を複数の要素に分割するように設計されています。

コード:

<?php
// Declaring a regex
$regex = "([0-9]+)";
// Defining the input string
$inputstr = "String_a 1 String_b 2 String_c 3";
//Splitting the input string based on matching regex expression
$result = preg_split ($regex, $inputstr);
// Displaying result
echo $result[0];
echo "\n";
echo $result[1];
echo "\n";
echo $result[2];
echo "\n";
?>

出力

関数 preg_split() は、要素「1」、「2」、「3」が境界としてマークされているため、入力文字列を 3 つの部分に分割しました。

PHP 正規表現

以上がPHP 正規表現の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP マジック定数次の記事:PHP マジック定数