0

Hi im trying to write a validation class using : regexp-me lib because of the answer of this post.

What did i do

String id = "123456789";

String pattern = "\\d{7,8}";

public boolean validate(String id,String pattern){


    RE regular_expresion = new RE("\\d{7,8}");

    return regular_expresion.match(id);
}

This code should have returned false with that "id" since the pattern just should accept 7 to 8 digits. However if i use id = "1234567" it return true , the code is accepting 7 or more digits.

The {m,n} is working as a {n,}.

Has someone had this problem before?

Community
  • 1
  • 1
AmirG
  • 615
  • 8
  • 18

1 Answers1

1

The expression \\d{7,8} means:

  • find anywhere within the string
  • a string consisting only of digits
  • with at least 7, but not more than 8 characters.

This is true for 1234567 as this is a string of 7 digits as well as for 123456789 because also this string contains a string with 7 or 8 digits.

Change the expression to ^\\d{7,8}$ to get a positive result only when the entire string from beginning to end consists of only digits and the string length is either 7 or 8.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Wow so the method return true even if it has a substring with that pattern. My bad thank you Mofi. – AmirG Jun 20 '14 at 13:31