<?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>Nepherte (dot) be &#187; Belgian Phone Number</title>
	<atom:link href="http://www.nepherte.be/tag/belgian-phone-number/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nepherte.be</link>
	<description>About Nepherte, Mosiah and the person behind</description>
	<lastBuildDate>Mon, 30 Aug 2010 18:45:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Regular expression for Belgian phone numbers</title>
		<link>http://www.nepherte.be/regular-expression-for-belgian-phone-numbers/</link>
		<comments>http://www.nepherte.be/regular-expression-for-belgian-phone-numbers/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 22:02:15 +0000</pubDate>
		<dc:creator>Nepherte</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Belgian Phone Number]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Regular Expression]]></category>

		<guid isPermaLink="false">http://www.nepherte.be/?p=608</guid>
		<description><![CDATA[I needed a regular expression to parse Belgian phone numbers for a program I am writing. At first sight regular expressions can be quite difficult for people to understand. Luckily I have a course at University that studies the mathematics behind regular expressions and the languages they define. A lovely exercise. Belgian phone numbers comply [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a regular expression to parse Belgian phone numbers for a program I am writing. At first sight regular expressions can be quite difficult for people to understand. Luckily I have a course at University that studies the mathematics behind regular expressions and the languages they define. A lovely exercise.</p>
<p>Belgian phone numbers comply with following specifications:</p>
<ul>
<li> The 2 possible formats are 01 234 56 78 and 012 34 56 78.</li>
<li>The first 2 or 3 digits, the zone number, always start with a zero.</li>
<li>The entire phone numer counts 9 digits.</li>
</ul>
<p>There exist multiple other notations such as 012/34.56.78 and 012/34.56.78. The strenght of a regular expression is that it can describe all these notations in one line. This easy regular expression will do the necessary, though will also accept 10 digit numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>You could however use a logical &#8216;or&#8217; between two expressions to make sure only 9 digit phone numbers are accepted:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Belgian cell phone numbers on the other hand have a slightly different format so another expression is necessary:</p>
<ul>
<li>The used format is 0412 34 56 67</li>
<li>The first 2 digits should always start with 04</li>
<li>The total number of digits should always be 10</li>
</ul>
<p>Again multiple notations exist to write down cell phone numbers. An expression that only accepts this format with the most commonly used notations is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>. <span style="color: #7a0874; font-weight: bold;">&#93;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\\d<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>A small program in java that uses these regular expressions could look like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Matcher</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Pattern</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PhoneNumberValidator <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Check for valid Belgian phone numbers</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isValidPhoneNumber<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[0](?:<span style="color: #000099; font-weight: bold;">\\</span>d{1,2})[/. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2,3})[. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2})[. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2})&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> m.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Check for valid Belgian cell phone numbers</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isValidCellPhoneNumber<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[0][4](?:<span style="color: #000099; font-weight: bold;">\\</span>d{2})[/. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2,3})[. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2})[. ]?(?:<span style="color: #000099; font-weight: bold;">\\</span>d{2})&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> m.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nepherte.be/regular-expression-for-belgian-phone-numbers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
