Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regex again - allow only 11 digits.  RSS feed

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't this work right??? It's driving me crazy!
I discovered the matches method but nothing....
I only want to verify that billId is 11 digits, if its not then
print Invalid BillId.
-----------------------------------------------------
String chkPattern = "[\\D{11}]";
String billId = "21200044440";
if (billId.matches(chkPattern))
System.out.println("Valid Bill ID: "+billId);
else
System.out.println("Invalid Bill ID: "+billId);
-----------------------------------------------------
~Dave
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\D is a non digit,
\d is a digit.
Try

HTH,
M
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That worked like a dream. So darn picky....D or d....
Ahhhhh... thanks again.
Dave
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a fairly common convention in regex. \w is word, \W is a non-word. \s is a space, \S is a non space, and so on.
IMO, learning a new skill requires that you 'pay' some depts: once these depts are paid, the skill is yours. Consider one regex debt taken off your tab
M
[ February 24, 2004: Message edited by: Max Habibi ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!