<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webcodez - The database of web programming tutorials &#187; regular expressions</title>
	<atom:link href="http://www.webcodez.net/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Sun, 14 Aug 2011 14:01:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>PHP Regular Expressions</title>
		<link>http://www.webcodez.net/php-mysql/php-regular-expressions/</link>
		<comments>http://www.webcodez.net/php-mysql/php-regular-expressions/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 19:21:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[preg_match_all]]></category>
		<category><![CDATA[preg_replace]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=750</guid>
		<description><![CDATA[Regular expressions can be used for matching or replacing certain patters inside a string. A simple example would be to check whether a certain word is inside a certain string and maybe on a certain position ( begin / end of string ). However also more complicated patterns can be made ( for example to [...]]]></description>
			<content:encoded><![CDATA[<p>Regular expressions can be used for matching or replacing certain patters inside a string. A simple example would be to check whether a certain word is inside a certain string and maybe on a certain position ( begin / end of string ). However also more complicated patterns can be made ( for example to verify whether a string is an URL address ).To do this, regular expressions can be used. Functions that make use of regular expressions in PHP are: <strong>preg_replace, preg_match and preg_match_all</strong>. These are the functions that will be used inside examples with regular expressions.</p>
<p>To match a regular expression pattern with a string, we may use the <strong>preg_match</strong> or <strong>preg_match_all</strong> function. The difference between these two functions is that preg_match stops searching for a match after one has occurred while preg_match_all continues to search for all matches. The preg_match function therefore is moreover used to check whether a pattern occurs inside a string, while the preg_match_all function is used to actually retrieve all matches with the pattern inside the string. So how do we set up a pattern? Therefore multiple symbols can be used. Here’s a table of the most common ones:</p>
<p>
<table id="wp-table-reloaded-id-1-no-1" class="wp-table-reloaded wp-table-reloaded-id-1">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">Symbol</th><th class="column-2">Meaning</th>
	</tr>
</thead>
<tbody>
	<tr class="row-2 even">
		<td class="column-1">^</td><td class="column-2">The word after this symbol should be at the beginning of the string to match.</td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1">$</td><td class="column-2">The word before this symbol should be at the end of the string to match.</td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1">*</td><td class="column-2">The expression before this symbol may occur any amount of times in the string to<br />
match.</td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1">?</td><td class="column-2">The expression before this symbol may or may not occur once.</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1">+</td><td class="column-2">The expression before this symbol should at least occur once.</td>
	</tr>
	<tr class="row-7 odd">
		<td class="column-1">{x}</td><td class="column-2">The expression before this symbol should occur x amount of times.</td>
	</tr>
	<tr class="row-8 even">
		<td class="column-1">{x1,x1}</td><td class="column-2">The expression before this symbol should occur atleast x1 amount of times,<br />
max x2 amount of times.</td>
	</tr>
	<tr class="row-9 odd">
		<td class="column-1">.</td><td class="column-2">Any character.</td>
	</tr>
	<tr class="row-10 even">
		<td class="column-1">|</td><td class="column-2">Either the expression before or after this symbol should match.</td>
	</tr>
</tbody>
</table>
 
<table id="wp-table-reloaded-id-2-no-1" class="wp-table-reloaded wp-table-reloaded-id-2">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">Expression</th><th class="column-2">Meaning</th>
	</tr>
</thead>
<tbody>
	<tr class="row-2 even">
		<td class="column-1">bar$</td><td class="column-2">The string to match should contain the word ‘bar’ at the end of the string.</td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1">^foo(.*)bar$</td><td class="column-2">The string to match should contain the word ‘foo’ at the beginning of the<br />
string AND the word ‘bar’ at the end of the string.</td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1">^[A-Za-z]*[0-9]*$</td><td class="column-2">The string to match should start with only letters and end with only numbers.</td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1">.*	</td><td class="column-2">Any amount of any characters.</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1">[A-Za-z]</td><td class="column-2">Any letter ( basicly: all the characters between the brackets are allowed).</td>
	</tr>
	<tr class="row-7 odd">
		<td class="column-1">[A-Za-z]*</td><td class="column-2">Any amount of letters ( but also: only letters ).</td>
	</tr>
	<tr class="row-8 even">
		<td class="column-1">[A-Z]|[a-z]</td><td class="column-2">Either a capital or a non-capital letter.</td>
	</tr>
	<tr class="row-9 odd">
		<td class="column-1">^foo</td><td class="column-2">The string to match should contain the word ‘foo’ at the beginning of the<br />
string.</td>
	</tr>
</tbody>
</table>
<br />
<strong>preg_match example</strong></p>
<pre name="code" class="php">

&lt;?php
//preg_match([pattern], [string], [match], [flag], [offset])

$pattern = “/^[A-Za-z0-9_]*@[A-Za-z0-9_]*\.com$/”;
$str1       = “match_example@provider.com”;
$str2       = “no_match_example@provider.nl”;
$str3       = “no-match@[test.com”;
$str4      = “example_match123@456web.com”;

if(preg_match($pattern, $str1)) {
echo “$str1 matches.”;
}else{
echo “$str1 does not match.”;
}

if(preg_match($pattern, $str2)) {
echo “$str2 matches.”;
}else{
echo “$str2 does not match.”;
}

if(preg_match($pattern, $str3)) {
echo “$str3 matches.”;
}else{
echo “$str3 does not match.”;
}

if(preg_match($pattern, $str4)) {
echo “$str4 matches.”;
}else{
echo “$str4 does not match.”;
}
?>
</pre>
<p><strong>preg_match_all example</strong></p>
<pre name="code" class="php">
&lt;?php
//preg_match_all([pattern], [string], [matches], [flag], [offset])
$pattern = “match[0-9]”;
$str         = “this is match1 and this is match2”;
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches); //prints the array containing all matches found
}
?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

