Regex for tollfree numbers in java

Posted by arinte on Stack Overflow See other posts from Stack Overflow or by arinte
Published on 2010-04-19T14:40:44Z Indexed on 2010/04/19 14:43 UTC
Read the original article Hit count: 429

Filed under:
|

I have this regex to test for telephone # that should be a toll free.

public static final Pattern patternTollFree = Pattern.compile("^((877)|(800)|(866)|(888))");

So I only want to get those # where the user may have left the 1 off of the front of the string, but I have tried several things and I can't get java to match.

public String changeRingTo( String changedRinger )
{
    if ( null == changedRinger || changedRinger.length() != 10)
        return changedRinger;
    if ( patternTollFree.matcher(changedRinger).region(0, 2).matches() )
        changedRinger = '1' + changedRinger;
    return changedRinger;
}

I can't get this 2nd test case below to succeed. What am I doing wrong?

assertEquals( "Regex not working", "8189091000", of.changeRingTo("8189091000"));
assertEquals( "Regex not working", "18769091000", of.changeRingTo("8769091000"));

© Stack Overflow or respective owner

Related posts about java

Related posts about regex