• Post Reply Bookmark Topic Watch Topic
  • New Topic

String checking in a big set of patterns  RSS feed

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I need to check for a input string, that can match against 1 or more patterns in Java..

I tried with if - else checking of the matching values..but i have more than 100+ checks to do and such a if-else is not good way to do it either..

Also looping through each string and checking the input string could be intensive.

Is there a simple way and whose performance is good as well.

Sample values are below(there are almost 100+ such pattern)

If I give the input as IP|VAR|D00|PB|TM|AB , i should get the 3 patterns matching... And i need to pick the values of each of the pattern ...



 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Kumar wrote:Sample values are below(there are almost 100+ such pattern)
If I give the input as IP|VAR|D00|PB|TM|AB , i should get the 3 patterns matching... And i need to pick the values of each of the pattern ...

Well, a few things:
1. Those "patterns" don't appear to be regexes, so you'll have to explain the significance of the '*' and '**'.

2. Your "value" appears to be preceded by "- ", which I'm presuming might appear in a "pattern", so you may have to find it explicitly. Alternatively, if there is at least one space before and after the '-', you could try something like:
String[] portion = line.split(" +- +");
or
String[] portion = line.split("\\s+-\\s+");
which should then put your "pattern" in portion[0] and the "value" in portion[1]. Either way, I suggest you have a look at String.split() for details.

If that isn't possible you may have to remove the trailing spaces from your pattern yourself before you try to do any kind of match with it.

That's as much as I can offer because we don't know enough about how your "pattern" works.

Winston
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can keep the patterns in a String[], and then loop over that. That way you only have a single if statement, and not one for each pattern.

It may be even better to keep the patterns in an external file that is read at startup. That way you don't need to change any code if you need to alter the list of patterns.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another bit to add. If your pattern is a hierarchy -
where each 'level' is denoted by the pipe ('|') then you could generate a Map of Maps of values to help limit your search to just those that match the current hierarchy. For example, break the string into pieces around the pipe, pop off the left-most piece and look in the map for any sub-levels. Pop off the next-left-most piece and look in the map for more sublevels that match, etc... until you run out of levels.

The way I might do it would be to build a class which represents the hierarchy and stores a map and value at each level. Then implements an addValue(String pattern, String value) method which finds the next sublevel to put the value in, or if there is no sublevel to put it in then stores it in itself. Then implement a getAllMatches(String pattern) method which follows the hierarchy of maps to find all levels which match and have a value, puts the values in a List, and returns it.

This is assuming a lot about what your pattern means and you would have to implement whatever 'matching' means in your patterns. But it would be a way of limiting the comparisons required.
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is relevant, but note that you can compile java.util.regex.Pattern objects which speed up matcher searches. Worth it if there are going to be many searches.

Bill
[edit=CR]Correct minor spelling error[/edit]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!