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

RegEx to verify variable contents/length  RSS feed

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a session variable called myID and I need to verify that this variable contains exactly 11 digits. How can I do this using a RegEx?
I've used the pattern class to match and replace words but I'm unsure how to check this variable.
Any little snippets that I can use as a guide for future RegEx patterns?
Thanks,
Dave
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a pattern, you can use \d to refer to a digit. And you can specify a number of repetitions using curly braces {}, e.g.
\d{5} - exactly five digits
a-z{3,10} - from three to ten lowercase letters
\w{4,} - at least four word characters
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^[0-9]{11}$ should match an integer of exactly 11 digits but will allow 0 as the initial digit. To prevent that then use ^[1-9][0-9]{10}$
The ^ matches the beginning of a line and the $ matches the end of a line so there can be nothing else in the String for these to match.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yingst again!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neener, neener.
The two posts do have some different info in them though, so all is not lost. I'll note that if you use matches() rather than find() (in the Matcher class) then it tries to match the entire input string, not just part of it, so the ^ $ would be unnecessary. But that only works if you know to use matches(), which I hadn't mentioned in the earlier post, so it's good that this came up.
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I had the correct syntax but it doesn't seem to be working.
Could someone lend me some eyes

import java.util.regex.*;
public class elevenDigitVariable {
public static void main(String[] args) {
try {
String billId = "21233343430";
Pattern p = Pattern.compile("[\\d{11}]");
Matcher m = p.matcher(billId);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result) {
deletedIllegalChars = true;
result = m.find();
}
if (deletedIllegalChars)
System.out.println("Invalid Bill ID: "+billId);
else
System.out.println("Valid Bill ID: "+billId);
}
catch (Exception ex) {
System.out.println(ex);
}
}
}
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, you're working too hard

HTH,
M
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave - FYI the [ indicates a character class - what follows is a list of the values a single character may have. Inside a character class, symbols are interpreted differently than they are outside a character class. The \d and {} notations work outside a character class, not inside it.
Of course, now you've got the answer, thanks to Max's generosity. But in the future it's important to be aware that [ changes how subsequent symbols are interpreted.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!