Regex Utility Class

10 April 2014

Sometimes it's useful to have in hand several utility type methods in order to do similar tasks repeatedly. Utility classes are good candidates for a singleton. This example shows a Singleton Regex Utility class that provides several Regex validation methods:

package io.github.guilhebl.util;

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class RegexUtil {

<span class="cm">/**
^                 # start-of-string
(?=.*[0-9])       # a digit must occur at least once
(?=.*[a-z])       # a lower case letter must occur at least once
(?=.*[A-Z])       # an upper case letter must occur at least once

### (?=.*[@#$%^&amp;+=])  # a special character must occur at least once

(?=[^\s]+$)       # no whitespace allowed in the entire string
.{6,}             # anything, at least eight places though
$                 # end-of-string
*/</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">PASSWORD_PATTERN</span> <span class="o">=</span> <span class="s">"^(?=.*[0-9])(?=.*[a-z])(?=[^\\s]+$).{6,}$"</span><span class="o">;</span>

<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">EMAIL_PATTERN</span> <span class="o">=</span> <span class="s">"^[_A-Za-z0-9-]+(\\."</span> <span class="o">+</span>
        <span class="s">"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*"</span> <span class="o">+</span>
        <span class="s">"(\\.[A-Za-z]{2,})$"</span><span class="o">;</span>

<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">FILE_NAME_PATTERN</span> <span class="o">=</span> <span class="s">"[_a-zA-Z0-9\\-\\.]+"</span><span class="o">;</span> 

<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">PHONE_PATTERN</span> <span class="o">=</span> <span class="s">"^[+]?([0-9]*[\\.\\s\\-\\(\\)]|[0-9]+){3,24}$"</span><span class="o">;</span>

<span class="kd">private</span> <span class="kd">static</span> <span class="n">RegexUtil</span> <span class="n">instance</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Pattern</span> <span class="n">patternPassword</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Pattern</span> <span class="n">patternEmail</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Pattern</span> <span class="n">patternFileName</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Pattern</span> <span class="n">patternPhone</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Matcher</span> <span class="n">matcher</span><span class="o">;</span> 

<span class="kd">private</span> <span class="nf">RegexUtil</span><span class="o">()</span> <span class="o">{</span>
    <span class="n">patternEmail</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">EMAIL_PATTERN</span><span class="o">);</span>
    <span class="n">patternFileName</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">FILE_NAME_PATTERN</span><span class="o">);</span>
    <span class="n">patternPhone</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">PHONE_PATTERN</span><span class="o">);</span>
    <span class="n">patternPassword</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">PASSWORD_PATTERN</span><span class="o">);</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kd">static</span> <span class="n">RegexUtil</span> <span class="nf">getInstance</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> 
    <span class="o">{</span>
        <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RegexUtil</span><span class="o">();</span>
    <span class="o">}</span>
    <span class="k">return</span> <span class="n">instance</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">validateIntegerID</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">id</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">id</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>          
        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">}</span>     
    <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">validateStringTypeRegex</span><span class="o">(</span><span class="n">String</span> <span class="n">text</span><span class="o">,</span> <span class="n">String</span> <span class="n">patternType</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">text</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">text</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">""</span><span class="o">))</span> <span class="o">{</span> 

        <span class="k">if</span> <span class="o">(</span><span class="n">patternType</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"email"</span><span class="o">))</span> <span class="o">{</span>
            <span class="n">matcher</span> <span class="o">=</span> <span class="n">patternEmail</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">text</span><span class="o">);</span>  
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">patternType</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"password"</span><span class="o">))</span> <span class="o">{</span>
            <span class="n">matcher</span> <span class="o">=</span> <span class="n">patternPassword</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">text</span><span class="o">);</span>   
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">patternType</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"phone"</span><span class="o">))</span> <span class="o">{</span>
            <span class="n">matcher</span> <span class="o">=</span> <span class="n">patternPhone</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">text</span><span class="o">);</span>  
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">patternType</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"fileName"</span><span class="o">))</span> <span class="o">{</span>
            <span class="n">matcher</span> <span class="o">=</span> <span class="n">patternFileName</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">text</span><span class="o">);</span>   
        <span class="o">}</span>

        <span class="k">if</span><span class="o">(</span><span class="n">matcher</span><span class="o">.</span><span class="na">matches</span><span class="o">()){</span>
            <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
        <span class="o">}</span>
    <span class="o">}</span>     
    <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">validateEmail</span><span class="o">(</span><span class="n">String</span> <span class="n">email</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nf">validateStringTypeRegex</span><span class="o">(</span><span class="n">email</span><span class="o">,</span> <span class="s">"email"</span><span class="o">);</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">validateFileName</span><span class="o">(</span><span class="n">String</span> <span class="n">fileName</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nf">validateStringTypeRegex</span><span class="o">(</span><span class="n">fileName</span><span class="o">,</span> <span class="s">"fileName"</span><span class="o">);</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">validatePhoneNumber</span><span class="o">(</span><span class="n">String</span> <span class="n">phone</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nf">validateStringTypeRegex</span><span class="o">(</span><span class="n">phone</span><span class="o">,</span> <span class="s">"phone"</span><span class="o">);</span>
<span class="o">}</span>

}

comments powered by Disqus