• Post Reply Bookmark Topic Watch Topic
  • New Topic

HELP!!!! with Java Regular Expressions  RSS feed

 
Joe Urbanek
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • 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.

  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!