• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

RegEx to verify variable contents/length

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yingst again!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, you're working too hard

HTH,
M
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic