| Author |
Matching a string with a list of values
|
Karthik Krishnamurthy
Ranch Hand
Joined: Feb 04, 2005
Posts: 118
|
|
Hi, I am in the midst of writing a validator. Basically, there are different tokens, each with multiple values. (For eg: HJ^23^SDF) with "^" being a delimiter separating the values inside a token. I need to ensure that each of these values falls within an accepted set of values, i.e For Token HJ, the first value can be <23,45,24,76> etc. I have about 20 tokens and between 3-8 values per token. What would be the best way to do this. Only thing I can think of is build multiple sets with the accepted values and check corresponding values with the appropriate set. i.e String[] pattern = {23,25,24,76}; Set set=new TreeSet(); for(i=0;i<pattern.length;i++) set.add(pattern[i]); And then set.contains(value) Is there a better/cleaner way to do this? Thank You Karthik
|
 |
Kaydell Leavitt
Ranch Hand
Joined: Nov 18, 2006
Posts: 679
|
|
|
I'm not sure that I understand the specification, but did you know that the String class has a method called split() that you can use to split a String when there is a single delimiter, such as "^"?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Actually, that should probably be "\\^", since ^ is a regex special character indicating the start of the string.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Karthik Krishnamurthy
Ranch Hand
Joined: Feb 04, 2005
Posts: 118
|
|
The "^" is an example value. Usually the strings I get are of the type HJ*23*SDF. I would be extracting each value using a tokenizer on the *. I would then have to compare each value with a predefined accepted set of values as mentioned in the original post. Is there any other way than what I have mentioned in the first post? Thanks a lot Karthik
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Well, there are many possible ways, but if I understand your specification correctly, I think using sets is probably best. I would probably use HashSets rather than TreeSets, unless there's some reason you need the set contents to be sorted. If you're just using contains, HashSet is usually faster. I would also use Kaydell's suggestion with String.split(). Note that "*" is another special character in regular expressions, which also requires escaping: It's also possible to construct a single regular expression for each token, that lists the allowed values. However this is probably hard to read, as well as less efficient. It woud look something like this: Although I'm not sure it's really less efficient. You can always try it both ways and see. Probably I would use whichever of those two looks easier for you to understand. Some people know regular expressions very well, in which case the regex solution may be good. In general though I would favor the HashSet solution. [ February 25, 2008: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Karthik Krishnamurthy
Ranch Hand
Joined: Feb 04, 2005
Posts: 118
|
|
Thanks Guys. I'll go with the hash set way. Only things is theres about 100 different types of elements each with about 4-5 different values. Adding them to the sets if going to be a pain! Thanks again. Karthik
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Ummm... can't you use loops for that sort of thing? It's a little longer if you don't have JDK 5 or 6, but the same idea applies - most of the work can be done in a method that you can reuse for each set. [ February 25, 2008: Message edited by: Jim Yingst ]
|
 |
Roger Chung-Wee
Ranch Hand
Joined: Sep 29, 2002
Posts: 1683
|
|
|
My instinct would be to declare a static Pattern for each token. I know that regular expressions are not familiar to most developers, but as they can be so useful it is well worth the effort to learn about this subject.
|
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
|
 |
Karthik Krishnamurthy
Ranch Hand
Joined: Feb 04, 2005
Posts: 118
|
|
Thanks for the inputs guys. I think I will go with looping for now, studying Patterns on the side for the second phase of this project. Thanks again Karthik
|
 |
 |
|
|
subject: Matching a string with a list of values
|
|
|