PHP Reguliere Expressies
Wat is een reguliere expressie?
Een reguliere expressie is een reeks tekens die een zoekpatroon vormt. Wanneer u zoekt naar gegevens in een tekst, kunt u dit zoekpatroon gebruiken om te omschrijven waarnaar u zoekt.
Een reguliere expressie kan een enkel teken zijn of een ingewikkelder patroon.
Reguliere expressies kunnen worden gebruikt om alle soorten tekstzoek- en tekstvervangingsbewerkingen uit te voeren.
Syntaxis
In PHP zijn reguliere expressies strings die zijn samengesteld uit scheidingstekens, een patroon en optionele modifiers.
$exp = "/w3schools/i";
In het bovenstaande voorbeeld /
is het scheidingsteken , is w3schools het patroon waarnaar wordt gezocht en i
is het een modifier die de zoekopdracht hoofdletterongevoelig maakt.
Het scheidingsteken kan elk teken zijn dat geen letter, cijfer, backslash of spatie is. Het meest voorkomende scheidingsteken is de schuine streep (/), maar als uw patroon schuine strepen naar voren bevat, is het handig om andere scheidingstekens te kiezen, zoals # of ~.
Reguliere expressiefuncties
PHP biedt een verscheidenheid aan functies waarmee u reguliere expressies kunt gebruiken. De
preg_match()
, preg_match_all()
en preg_replace()
functies zijn enkele van de meest gebruikte:
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() gebruiken
De preg_match()
functie vertelt je of een string overeenkomsten met een patroon bevat.
Voorbeeld
Gebruik een reguliere expressie om hoofdletterongevoelig te zoeken naar "w3schools" in een string:
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
Preg_match_all() gebruiken
De preg_match_all()
functie vertelt je hoeveel overeenkomsten er zijn gevonden voor een patroon in een string.
Voorbeeld
Gebruik een reguliere expressie om hoofdletterongevoelig te tellen van het aantal keren dat "ain" in een tekenreeks voorkomt:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>
Preg_replace() gebruiken
De preg_replace()
functie vervangt alle overeenkomsten van het patroon in een string door een andere string.
Voorbeeld
Gebruik een hoofdletterongevoelige reguliere expressie om Microsoft te vervangen door W3Schools in een tekenreeks:
<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo
preg_replace($pattern, "W3Schools", $str); // Outputs "Visit
W3Schools!"
?>
Modifiers voor reguliere expressies
Modifiers kunnen wijzigen hoe een zoekopdracht wordt uitgevoerd.
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 |
Reguliere expressiepatronen
Haakjes worden gebruikt om een reeks tekens te vinden:
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 |
metakarakters
Metatekens zijn tekens met een speciale betekenis:
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 |
Kwantificatoren
Kwantificatoren definiëren hoeveelheden:
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 |
Opmerking: als uw uitdrukking moet zoeken naar een van de speciale tekens, kunt u een backslash ( \ ) gebruiken om eraan te ontsnappen. Als u bijvoorbeeld naar een of meer vraagtekens wilt zoeken, kunt u de volgende uitdrukking gebruiken: $pattern = '/\?+/';
Groepering
U kunt haakjes ( )
gebruiken om kwantoren toe te passen op hele patronen. Ze kunnen ook worden gebruikt om delen van het patroon te selecteren die als een match moeten worden gebruikt.
Voorbeeld
Gebruik groeperen om naar het woord "banaan" te zoeken door te zoeken naar ba gevolgd door twee instanties van na :
<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
Volledige RegExp-referentie
Ga voor een volledige referentie naar onze Complete PHP Regular Expression Reference .
De referentie bevat beschrijvingen en voorbeelden van alle reguliere expressiefuncties.