Regex for tollfree numbers in java
- by arinte
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"));