<?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>Coding Friends &#187; regular expression</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/regular-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CDATA, With, Match and Regular Expression</title>
		<link>http://www.codingfriends.com/index.php/2010/08/10/cdata_with_match_regular_expression/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cdata_with_match_regular_expression</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/10/cdata_with_match_regular_expression/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 20:38:47 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[CDATA]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1140</guid>
		<description><![CDATA[CDATA Sometimes when you get errors within your HTML validation can sometimes just get bogged down with javascript code that has not been &#8220;told&#8221; to be ignored by the parser, for example the javascript code if &#40;a &#60; b&#41; is fine within that context, but within HTML the < could be a start of a [...]]]></description>
			<content:encoded><![CDATA[<p><b>CDATA</b></p>
<p>Sometimes when you get errors within your HTML <a href="http://validator.w3.org/">validation</a> can sometimes just get bogged down with javascript code that has not been &#8220;told&#8221; to be ignored by the parser, for example the javascript code</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;</span> b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>is fine within that context, but within HTML the < could be a start of a tag and thus would fall over with some old parsers and also the validator, so best to wrap the javascript code within the CDATA</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!</span><span style="color: #009900;">&#91;</span>CDATA<span style="color: #009900;">&#91;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;</span> b<span style="color: #009900;">&#41;</span> 
...
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p><b>WITH</b></p>
<p>The with syntax will allow you to work on a object of a HTML form elements within having to constantly referencing the object fully, for example if you had a HTML as</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;theform&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;theinput&quot;</span><span style="color: #66cc66;">/</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondinput&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>and so within the javascript you could do something like this without using the with statement,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> theinput <span style="color: #339933;">=</span> document.<span style="color: #660066;">form</span>.<span style="color: #660066;">theform</span>.<span style="color: #660066;">theinput</span><span style="color: #339933;">;</span></pre></div></div>

<p>well if you wanted to you could use the &#8220;with&#8221; syntax and thus if you wanted to access all of the elements within that HTML object then</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">with</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">form</span>.<span style="color: #660066;">theform</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> first <span style="color: #339933;">=</span> theinput<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> second <span style="color: #339933;">=</span> secondinput<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>just allot more nicer to use the objects <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><b>Match / Regular expression</b></p>
<p>Since a string within the javascript is capable of having regular expression actions applied to it, you can use the inbuilt method that is associated with the string class as such, which allows you to do regular expression, but for some reason you do not need to put in the &#8221; &#8221; around the regular expression.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> username <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>
    username.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span>.<span style="color: #339933;">+@</span>.<span style="color: #339933;">+</span>\.<span style="color: #660066;">com</span>$<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
....</pre></div></div>

<p>will match with a regular expression so that the username has to end with .com for it to be valid!!.. not really a good test, but there you go!.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/10/cdata_with_match_regular_expression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>preg_match &#8211; regular expression</title>
		<link>http://www.codingfriends.com/index.php/2010/07/29/preg_match-regular-expression/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=preg_match-regular-expression</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/29/preg_match-regular-expression/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 10:03:55 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1128</guid>
		<description><![CDATA[Here is a good link for a cheat sheet for regular expressions that if you forget some of the syntax. Here are some quick notes about regular expression in PHP that allow you to pull out information from a string of text. $stringofvalues = &#34;hi there from me - and now after that lets!!&#34;; preg_match&#40;&#34;/^.* [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a good link for a <a href="http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/">cheat sheet</a> for regular expressions that if you forget some of the syntax.</p>
<p>Here are some quick notes about regular expression in PHP that allow you to pull out information from a string of text.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$stringofvalues</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hi there from me - and now after that lets!!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^.* - (.*)!!/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stringofvalues</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and the output would be</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> hi there from me <span style="color: #339933;">-</span> and now after that lets<span style="color: #339933;">!!</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> and now after that lets
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>where the first array [0] is always the string that was passed in, and any matches that you picked are in the following index&#8217;s in the array (because the first match [0] in the array is the real string because it has matched that already <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) and then the second matches that you want to check for are within the regular expression ( .. ) brackets, so in my regular expression</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #339933;">.*</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!!/</span></pre></div></div>

<p>this means start at the beginning of the string &#8216;^&#8217; and then any character &#8216;.&#8217; 0 or more times &#8216;*&#8217; find un-till you find a string as &#8221; &#8211; &#8221; (which is within the string to be searched) and then now copy any character 0 or more times &#8216;.*&#8217; but this time place it into a matched index within the array by using the () around that regular expression search.</p>
<p>So if you wanted to pull back 2 matches from that string, then you could do something like</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(.*) - (.*)!!/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stringofvalues</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which the output would be</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> hi there from me <span style="color: #339933;">-</span> and now after that lets<span style="color: #339933;">!!</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> hi there from me
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> and now after that lets
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>where the first part of the string (before the &#8221; &#8211; &#8220;) is within the 1st index of the matches array and then the second search match is within the 2nd index of the matches array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/29/preg_match-regular-expression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular expression &#8211; PHP</title>
		<link>http://www.codingfriends.com/index.php/2010/01/22/regular-expression-php/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=regular-expression-php</link>
		<comments>http://www.codingfriends.com/index.php/2010/01/22/regular-expression-php/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:17:24 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=561</guid>
		<description><![CDATA[Regular expression is when you have a match condition and a string to search through. Here is a cheat sheet to download to view regular expressions syntax. The regular matching method in php that I use is preg_match_all int preg_match_all &#40; string pattern, string subject, array &#38;matches &#91;, int flags &#91;, int offset&#93;&#93; &#41; which [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://uk3.php.net/preg_match_all"><a href="http://en.wikipedia.org/wiki/Regular_expression">Regular expression</a> is when you have a match condition and a string to search through.  Here is a <a href="http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/pdf/">cheat sheet</a> to download to view regular expressions syntax.</p>
<p>The regular matching method in php that I use is preg_match_all</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">int <span style="color: #990000;">preg_match_all</span> <span style="color: #009900;">&#40;</span> string pattern<span style="color: #339933;">,</span> string subject<span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #339933;">&amp;</span>matches <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int flags <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int offset<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>which basically means that it will return a integer value of how many matches it has found within the subject text.  The flags and offset are optional values.</p>
<p>Here is a example to pull out HTML A links within a text, A links start with &lt;a and have some optional parameters e.g. href, title &gt;text to display on the screen&lt;/a&gt; (the closing tag.  So in xml speak as such</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span><span style="color: #66cc66;">&#91;</span>optional attributes<span style="color: #66cc66;">&#93;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>value<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So the basics of searching for HTML A links within a string is to look for &#8220;<a" and then 0-x amount of characters that ends with ">&#8221; which then has the value and then the closing tag </a>.  So to put that in regular expression talk, to break it down we first search for the start <strong>&lt;a</strong> followed by x amount of characters <strong>\s.*</strong> with a ending tag of <strong><\/a></strong> the &#8220;\&#8221; is there because the / is a condition statement for regular expression.  And with this together it would look like</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a\s<span style="color: #339933;">.*&lt;</span>\<span style="color: #339933;">/</span>a<span style="color: #339933;">&gt;</span></pre></div></div>

<p>But because <\/a> happens twice in the string and either one can be the end point of the search since we are searching string values &#8220;\s.*&#8221; 0-x amount of them.  \s means white space character &#8220;.&#8221; means any character that is not \n and &#8220;*&#8221; means 0 &#8211; more times of the previous check.  So there is potential for a problem here we are not telling the regular expression to stop at the first < character and to this we use the [] to match some characters, the ^ in this context means any character not in this list about to come and "<" is the stopping character.  So the full regular expression will be.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a\s<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*&lt;</span>\<span style="color: #339933;">/</span>a<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Here is the php code that will display all of the HTML A links from a string.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// here is a string with 2 a href links inside it and sounding text.</span>
<span style="color: #000088;">$matchAStr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;coding friends is my site :) &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.codingfriends.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;codingfriends.com&lt;/a&gt; hi there.. &quot;</span> <span style="color: #339933;">.</span> 
	    <span style="color: #0000ff;">&quot; mountains are nice. &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.norfolkhospice.org.uk/<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;norfolkhospice.org.uk&lt;/a&gt;&quot;</span> <span style="color: #339933;">.</span> 
	    <span style="color: #0000ff;">&quot; support the tapping house in Norfolk&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the preg_match_all uses regular expression to match and pull out strings, $matches is the matches found.</span>
&nbsp;
<span style="color: #000088;">$matched</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&lt;a\s.[^&lt;]*&lt;\/a&gt;/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$matchAStr</span><span style="color: #339933;">,</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;There was <span style="color: #006699; font-weight: bold;">$matched</span> matche(s)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ol&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ol&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">There was 2 matche(s)
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codingfriends.com&quot;&gt;codingfriends.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.norfolkhospice.org.uk/&quot;&gt;norfolkhospice.org.uk&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/01/22/regular-expression-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

