https://regex101.
com/
Delimiter
any character to start and end it, except \s or \
Meta Characters
^ Marks the start of a string
$ Marks the end of a string
| Boolean OR
\s Any white-space character [\r\n\t\f ]
\w Any alpha numeric character plus underscore. Equals to [A-Za-z0-9_]
\W Any non alpha numeric characters
\s ny white-space character
\S Any non white-space character
\d Any digits. Equals to [0-9]
\D Any non digits. Equals to [^0-9]
grouping:
() Group elements
(?!ab|cd).* Group of elements except ab or cd (this is negative lookahead)
[abc] Item in range (a,b or c)
[^abc] NOT in range (every character except a,b or c)
a{2} Exactly two of a
a{,5} Up to five of a
a{5,10} Between five to ten of a
(?:([^.]+))? optional group
named match:
$test = "yet another test";
preg_match( '/(?P<word>t[^s]+)/', $test, $matches );
var_dump( $matches['word'] );
repetition:
? 0 | 1
+ 1 | inf
* 0 | inf
. Matches any single character except new line
a* Zero or more of a
.* Zero or more of any character
a? Zero or one a characters ( equals to a{0,1} )
.? Zero or one of any character
a+ One or more of a
.+ One or more of any char
*? Quantifier - repeats previous token zero to infinite times
npr: .*? - any char zero to infinite times
.* matches any character [unicode]
Quantifier: * Between zero and unlimited times, as many times as possible,
giving back as needed [GREEDY]
.*? matches any character [unicode]
Quantifier: *? Between zero and unlimited times, AS FEW TIMES AS POSSIBLE,
expanding as needed [LAZY]
matchings:
[^@~] bilo koji znak osim @~
[^@~]* niz bilo kojih znakova osim @~
([^@~]*) capture niz bilo kojih znakova osim @~
["\\\'\s+] Match a single character present in the list below
" a single character in the list " literally (case insensitive)
\\ matches the character \ literally
\' matches the character ' literally
\s match any white space character [\r\n\t\f ]
+ the literal character +
Escape characters - special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | :
Modifiers
/.../uims
u Pattern is treated as UTF-8
x mogu se koristiti razmaci u patternu, bez da se match-aju (također i
comments #..., i newline, pa se može pisati jako lijep i pregledan regex)
i case insensitive
m multiline, ignores new line
s if included, the . character matches all characters, including newline
=================
preg_match
=================
- traži postoji li match u subject-u
- vraća vrijednosti:
1 found
0 not found
false error
- ie1:
if ( preg_match( $pattern, $subject ) == 1 ) { ... }
- ie2:
if ( preg_match( $pattern, $subject, $matches ) == 1 ) { ... }
ako je zadan $matches, napuni sa vrijednostima matcheva ovako:
echo( "Entire match: ${matches[0]}" );
echo( "1. group: ${matches[1]}" );
echo( "2. group: ${matches[2]}" );
echo( "3. group: ${matches[3]}" );
=================
preg_split
=================
- explodira u array
- ie.
$txt = ',,text,other,,end.';
$words_Also_Empty_Arr = preg_split( '/,/', $txt, -1);
$words_Without_Empty_Arr = preg_split( '/,/', $txt, -1,
PREG_SPLIT_NO_EMPTY );
- PREG_SPLIT_NO_EMPTY - ne vraća prazne
=================
preg_grep
=================
- radi regex nad elementima arraya i vraća array sa tim elementima
- ie.
$foods = array("pasta", "steak", "fish", "potatoes");
$p_foods_Arr = preg_grep("/p(\w+)/", $foods);
$non_p_foods_Arr = preg_grep("/p(\w+)/", $foods, PREG_GREP_INVERT);
=================
preg_quote
=================
- escapes every regex character
- ie.
$punctuation = preg_quote( "().-" );
echo $punctuation; // \(\)\.-.
=================
preg_replace
=================
- regex search and replace
// pattern is a little different -- just specifies the search part, not
before and after
$pattern = '|(/wp-content/uploads/\d{4}/\d{2}/)\d{8}/|';
$replace = '$1'; // replace with the first match
$content_updated = null;
$content_updated = preg_replace( $pattern, $replace, $content );
if ( $content_updated && $content !== $content_updated ) {
...
}