| Author |
Regular expression
|
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
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
|
-Chiru
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
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
Joined: Feb 18, 2008
Posts: 283
|
|
right now i split the string from ","
and split according to "-"
i'm facing problem if -ve value comes
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
|
|
Agree with Pete.
Moving.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
"-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 ?
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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 "-".
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
|
refer to the interpreter pattern interpreter pattern java
|
My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
Thanks for the reply
i got the solution
Where rawdata is the input string
|
 |
Piet Verdriet
Ranch Hand
Joined: Feb 25, 2006
Posts: 266
|
|
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
Joined: Feb 18, 2008
Posts: 283
|
|
Ya!
Thanks for suggestion
|
 |
 |
|
|
subject: Regular expression
|
|
|