Regex – Cheat

function regex  to find pattern from string

find pattern with function  preg_match(“/your pattern/”, “your string here”);

[ ] = class string


$string = 'big';

echo preg_match("/b[aoiu]g/", $string, $matches); // 1

echo preg_match("/b[a-f]g/", $string, $matches); // 1

echo preg_match("/b[0-9]g/", $string, $matches); // 0

$string = 'bXg';

echo preg_match("/b[a-zA-Z]g/", $string, $matches); // 1

i = insensitive


$str = "BaNG"

echo preg_match('/[A-Z]/i', $str); // 1

+ = one or more pattern


// create a string
$string = 'pp';

// look for a match
echo preg_match("/ph+p/", $string, $matches);
// The above script will echo 0.
// This is because the h character did not appear one of more times in the string.

<pre>

* = zero or more pattern


// create a string

$string = 'php';

// look for a match
echo preg_match("/ph*p/", $string, $matches);
/*
result is 1 as we have found 1 match.
In the above example its match was with one h character.
This same regex would match pp (zero h characters), and phhhp (3 h characters).
*/

 

? = if have or not is 1, if have but different is 0
$string = '12345678';
echo preg_match("/1234-?5678/", $string, $matches); // 1

$string = '1234-5678';
echo preg_match("/1234-?5678/", $string, $matches); // 1

$string = '1234+5678';
echo preg_match("/1234-?5678/", $string, $matches); // 0

{} = length
$string = 'PHP123';
echo preg_match("/PHP[0-9]{3}/", $string, $matches);
\d – Matches any numeric character – same as [0-9]
\D – Matches any non-numeric character – same as [^0-9]
\s – Matches any whitespace character – sames as [ \t\n\r\f\v]
\S – Matches any non-whitespace character – same as [^ \t\n\r\f\v]
\w – Matches any alphanumeric character – same as [a-zA-Z0-9_]
\W – Matches any non-alphanumeric character – same as [^a-zA-Z0-9_]
$string = 'ab-ce*fg@ hi & jkl(mnopqr)stu+vw?x yz0>1234<567890';
preg_match_all("/[\w]/", $string, $matches);
foreach($matches[0] as $value)
echo $value;   //  abcefghijklmnopqrstuvwxyz01234567890
<code>$string = '2 bad for perl';
if(preg_match("/^\d/", $string)) echo 'String begins with a number';
else echo 'String does not begin with a number';
// String begins with a number
$string = 'abcdefghijklmnopqrstuvwxyz0123456789';
if(preg_match("/./", $string))
echo 'The string contains at least on character';
else
echo 'String does not contain anything';
// <code>The string contains at least on character
| = pipe or
// a simple string
$string = "This is a Hello World script";
// try to match the patterns This OR That OR There
echo preg_match("/^(This|That|There)/", $string); // 1
i – Ignore Case, case insensitive
U – Make search ungreedy
s – Includes New line
m – Multiple lines
x – Extended for comments and whitespace
e – Enables evaluation of replacement as PHP code. (preg_replace only)
S – Extra analysis of pattern
b – Word Boundry
B – Not a word boundary
A – Start of subject
Z – End of subject or newline at end
z – End of subject
G – First matching position in subject
</div>
<div>$string = 'I live in the whitehouse';
echo preg_match("/(?<=white)house/i", $string); // 1
echo preg_match("/(?<!white)house/i", $string); // 0</div>
<div>
   *  zero or more characters, same as {0, }
    +  1 or more characters, same as {1, }
    ?  zero or one character, same as {0,1}

</div>
<div>/*** get the host name from a url ***/
preg_match('#^(?:http://)?([^/]+)#i', "http://www.phpro.org/tutorials", $matches);

/*** show the host name ***/
echo $matches[1];</div>
<div></div>
<div>// www.phpro.org</div>
<div>

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s