• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression string format check.  RSS feed

 
Sanj Sharma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i want to use regular expression for my application. I have some kind of string which i am reading from console and after reading i have to check the parameters of that string like e.x

YOU:ME 3,OTHERS 4,EVERYONE 5
So after reading from the console i have string "YOU:ME 3,OTHERS 4,EVERYONE 5" but now from this string i want to check the ranges for ME, OTHERS and EVERYONE (suppose iset range for ME 0-5, OTHERS 0-9 etc.) and also the pattern of the string suppose if someone enter pattern "YOU:My 3,OTHrtrERS 4,EVERYONE 5" on screen i will verify if this is the correct format of string as defined above or not.
Can anyone help me here???
 
P Reen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

have a look at http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

Also your code should look something like this

if ( Pattern.matches("YOU:ME [1-3], OTHERS [0-9], EVERYONE [0-5]",
"YOU:ME 3, OTHERS 4, EVERYONE 5"))
{
System.out.println("Matches");
}
else
{
System.out.println("Does Not Match");
}

However this will only match on the correct spacing and lettering ( CAPS )
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!