Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression

 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i need a regular expression to parse the String which is entered from text box

like "-40.23-50.45,-30,40,<30,>50"
where i should consider "," is different set of values
and
"-40.23-50.45" means i have to split it as -40.23 to 50.45
"-30" as -30
"40" as 40
"<30" means values less then 30
">50" means values greater then 50

Thanks in advance
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't have anything to do with Swing (yes, you get the String from a GUI text field, but that really doesn't matter in the String's parsing), and I'm going to ask the moderators to move this to one of the main Java forums. My question for you is: what have you tried so far and in what way does your current code not work?

Best of luck.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right now i split the string from ","
and split according to "-"

i'm facing problem if -ve value comes
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Pete.
Moving.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"-40.23-50.45" means i have to split it as -40.23 to 50.45


Is it 40.23 to 50.45 ? Both have - in front of them. BTW, show us what you have done already and what problem you are having now ?
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this if we give -40-50 means -40 is the min and 50 is the max for which is not working
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The split method in line 14 of your code probably does not do what you think it does:

String[] si=s.split("-",2);

It looks like you think that this looks from position 2 in the string onward. But this is not what it does. Look it up in the API documentation:
public String[] split(String regex, int limit)

Splits this string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.


You'd see that if the input is "-40-50", the call to split in line 14 results in two strings: "" and "40-50" - because it has done the split one time, around the first "-".
 
salvin francis
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
refer to the interpreter pattern interpreter pattern java
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

i got the solution


Where rawdata is the input string


 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chiranjeevi Kanthraj wrote:Thanks for the reply

i got the solution


Where rawdata is the input string




There is no need to recompile the pattern each time. You can put it above the for statement.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya!

Thanks for suggestion
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic