• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

HELP!!!! with Java Regular Expressions

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a java application that needs to validate the format of a String. I am attempting to use Java regular expressions to validate this String. If the String does not follow the pattern then an exception is thrown.

The String needs to follow the following format:

XXXX[@XXXX='XXXXXX']

*note the the XXXXs represent a series of any alpha nnumeric or special characters of unspecified length.

I am not sure if my Regular Expression pattern is correct.

Any assistance is appreciated.

Here is what I am tying to do:

public void isValid(){
String expression = "XXXX[@XXXX='XXXXXX']"; //valid
String expression2 = "XXXX[@XXXX='"; //invalid

Pattern expPattern =
Pattern.compile(".*\\w\u005B@.*\\w='.*\\w'\u005D");

String[] exps = new String[]{ expression, expression2};

for(int i = 0; i < exps.length; i++){
boolean valid = false;
Matcher baseMatcher = expPattern.matcher(expression );
valid = baseMatcher.find();
baseMatcher.reset();
System.out.printline(i + ": " + valid);
}
}
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your pattern is matching your invalid string. Did you try printing out the results so you could see what it was doing?

Looks like your square brackets need to be escaped (see below, I added another \ to the pattern and the matching was then as expected). For your information, I didn't check anything about your pattern other than it should match valid and not match invalid test cases.

Here's the code I used to check it, written while going through this Regex Tutorial (pdf) called Regex Recipes V 1.0:



Output:

RegexTutorial.find()
'.*\w[@.*\w='.*\w']' found in 'XXXX[@XXXX='XXXXXX']'
m.group(): 'XXXX[@XXXX='XXXXXX''

RegexTutorial.find()
'.*\w[@.*\w='.*\w']' found in 'XXXX[@XXXX=''
m.group(): 'XXXX[@XXXX='

RegexTutorial.find()
'.*\w\u005B@.*\w='.*\w'\u005D' found in 'XXXX[@XXXX='XXXXXX']'
m.group(): 'XXXX[@XXXX='XXXXXX']'

RegexTutorial.find()
'.*\w\u005B@.*\w='.*\w'\u005D' not found in 'XXXX[@XXXX=''
[ March 17, 2005: Message edited by: Carol Enderlin ]
 
Joe Urbanek
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • If you're validating the entire input, you should be using matches() rather than find().
  • Assuming that by "alphanumerics and special characters" you mean word characters (letters, digits, or underscores), you would use "\\w+" to match them (or, if they're optional, "\\w*").
  • Unicode escapes are useless for escaping regex metacharacters; just use a double backslash and the actual character.

  •  
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic