Studyon Minte9.com
ZCE 5.3

Study

RegExp



	// preg_match(//, str) [, &matches] | BOOLEAN or ZERO
	// delimiters: //, ||, && (any character)
	// metacharacters: . | ^$ | sdw | [] (group) | [-] range
	// quatifiers: * + ? {n,m}
	// subexpresions: (ab.d) - capture
	// multiple: preg_match_all
	// replace: preg_replace (simple or arrays)

	// exclude some strings from matching
		// preg_match - #/((?!xy|zw)[0-9a-z-]+)/[0-9a-z-]+/#
	// split a string by camel case
		// preg_split - (?=[A-Z])
	// make slug
		// preg_replace - /[^0-9a-z]/
	// truncate with word boundary
		// preg_match - /^.{1,10}b


preg_match()

<?php // The simplest possible regular expression is one that matches only one string // strpos is faster echo preg_match("/cde/", "abcde", $matches); // Output: 1 // $matches = Array ( [0] => cde ) echo strpos("abcde", "cde"); // Output: 2 // faster than preg_match
 
<?php // This function may return Boolean FALSE, // but may also return a non-Boolean value which evaluates to FALSE if (preg_match("/[a-zA-Z]/", "John Smith") === FALSE) { // Invalid name }
  Delimiters
<?php // any character can be used echo preg_match("/[a-z]/", "abcde"); // common delimiter echo preg_match("&[a-z&]&", "abcde"); // escaped delimiter
  Metacharacters
<?php // Every metacharacter represents a single character in the matched expression // Any character /./ echo preg_match("/./", "abc"); // 1 // match any character // return TRUE // Start /^/ echo preg_match("/^bc/", "abc"); // 0 // match start of the string, then // match b, match c // return FALSE // End /$/ echo preg_match("/c$/", "abc"); // 1 // c, end of the string // Whitespace /s/ echo preg_match("/asbc/", "a bc"); // 1 // match a, space, b, c // Digit /d/ echo preg_match("/d/", "a123bc"); // 1 // match any digit // Word /w/ echo preg_match("/w/", "abc 120"); // 1 echo preg_match("/w/", "a !"); // 1 echo preg_match("/w/", "!"); // 0 // match any word char
 
<?php // Metacharacters can also be expressed using grouping expressions // Group /[]/ echo preg_match("/b[cd]e/", "abcde"); // 0 // match a, c or d, e // returns FALSE echo preg_match("/b[cd]e/", "abce"); // 1 echo preg_match("/b[dc]e/", "abce"); // 1
 
<?php // You can also use ranges of valid characters inside a grouping expression // Range /[-]/ echo preg_match("/ab[c-ed]d/", "abc2d"); // 0 // match a, b, (c || d || e || digit), d echo preg_match("/ab[c-ed]d/", "ab2d123"); // 1
  Quantifiers A quantifier allows you to specify the number of times a particular character or metacharacter can appear in a matched string.
<?php // zero+ /[]*/ echo preg_match("/a*/", "abaa"); // 1 // match 'a' zero or more times echo preg_match("/a*/", "c"); // 1 echo preg_match("/a[b-d]*/", "acde"); // 1 // one+ /[]+/ echo preg_match("/a+/", "abaa"); // 1 // match 'a' one time or more times echo preg_match("/a+/", "c"); // 0 echo preg_match("/a[b-d]+/", "acee"); // 1 // 0|1 /[]?/ echo preg_match("/c?/", "aabaa"); // 1 // match 'c' zero or one times echo preg_match("/ac?b/", "abc"); // 1 echo preg_match("/ac?b/", "accbc"); // 0 // c matched two times // returns FALSE // Interval /{n,m}/ echo preg_match("/ab{0,1}c/", "abc"); // 1 // match 'a' minim zero and maxim 1 times echo preg_match("/ab{0,1}c/", "abbc"); // 0 // b matched 2 times // returns FALSE // Either parameter can be omitted to indicated a minimum limit with no maximum, // or a maximum limit without a minimum, but not both echo preg_match("/a{1}/", "abaac"); // 1 echo preg_match("/a{2}/", "abaac"); // 1 echo preg_match("/a{3}/", "abaac"); // 0 echo preg_match("/ab{0,}c/", "abbc"); // 1 // match a, b minim zero times, c echo preg_match("/ab{,1}c/", "abbc"); // 0 // match a, b maxim one times, c // returns FALSE // !!! {, maximum} is tricky (more research needed)
  Sub-Expersions A sub-expression is a regular expression contained within the main regular expression (or another sub-expression)
<?php echo preg_match("/a(bc.)e/", "abcxe"); // 1 // matches: a, b, c, any chars, e echo preg_match("/a(cb)e/", "abce"); // 0 echo preg_match("/(bc)/", "abce"); // 1 echo preg_match("/a(bc)d/", "abcd"); // 1 echo preg_match("/a[bc]d/", "abcd"); // 0 // don't confuse sub-expresion with grouping echo preg_match("/[a-z0-9_.]+@[a-z0-9_.]+(.com)/", "my@yahoo.com"); // 1 echo preg_match("/[a-z0-9_.]+@[a-z0-9_.]+(.com)/", "my@yahoo.ro"); // 0
  Capture Sub-expressions can also be used as capturing patterns
<?php $matches = array(); echo preg_match("/([a-z0-9_.]+)@([a-z0-9_.]+)(.com)/", "my@yahoo.com", $matches); // Output: 1 (TRUE) print_r($matches); // Array ( [0] => my@yahoo.com [1] => my [2] => yahoo [3] => .com )
  Multiple matches The preg_match_all() function allows you to perform multiple matches on a given string based on a single regular expression.
<?php $matches = array(); if (preg_match_all("/([abc])d/", "a1bb b2cc c3dd", $matches)) { var_dump($matches); } /* array(2) { [0]=> array(3) { [0]=> string(2) "a1" [1]=> string(2) "b2" [2]=> string(2) "c3" } [1]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } } */
  Replace matches Whilst str_replace() is quite flexible, it still only works on "whole" strings, that is, where you know the exact text to search for. Using preg_replace(), however, you can replace text that matches a pattern we specify.
<?php echo preg_replace("/a/", "x", "abc"); // xbc echo preg_replace("/a{2}/", "x", "abaac"); // abxc
  It is even possible to reuse captured subpatterns directly in the substitution string by prefixing their index with a dollar sign.
<?php echo preg_replace("|[b](.*)[/b]|", "<b>$1</b>", "[b]abc[/b]"); // <b>abc</b>
  Just like with str_replace(), we can pass arrays of search and replacement arguments. However, unlike str_replace(), we can also pass in an array of subjects on which to perform the search-and-replace operation. This can speed things up considerably, since the regular expression (or expressions) are compiled once and reused multiple times.
<?php $array = array("[b]abc[/b]", "[i]abc[/i]"); $result = preg_replace( array( "|[b](.*)[/b]|", "|[i](.*)[/i]|", ), array( "<b>$1</b>", "<i>$1</i>", ), $array ); print_r($array); // Array ( [0] => [b]abc[/b] [1] => [i]abc[/i] ) print_r($result); // Array ( [0] => <b>abc</b> [1] => <i>abc</i> ) // Notice how the resulting array maintains the array structure // that we passed in, which, however, is not passed by reference, nor is itmodified.
 


http://php.net/manual/en/reference.pcre.pattern.modifiers.php
https://www.cs.tut.fi/~jkorpela/perl/regexp.html