| Author |
Wildcard Search on Set
|
Tim Jones
Greenhorn
Joined: Apr 06, 2008
Posts: 28
|
|
I am looking for an example of how to achive the following.
For example:
I have the following ip's in a Set:
111.112.113.114;
112.113.114.114;
......
111.112.113.115
The values are all consistant IP format.
now when I search for say *.112.113.* through the standard input
I want to get back:
111.112.113.114
111.112.113.115
e..g match the 2nd and 3rd octets and perform wildcard search on 1st and 4th octets
and
*.*.*.* <-- will output all IP addresses
* = wildcard
Thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
You can try writing a specialised equals() method.
You are more likely to find a regular expression helpful.
|
 |
Tim Jones
Greenhorn
Joined: Apr 06, 2008
Posts: 28
|
|
Campbell Ritchie wrote:You can try writing a specialised equals() method.
You are more likely to find a regular expression helpful.
I am not that familiar with regex (only basics), I could do with some help in constructing the right regex pattern to match against the elements of the set.
this is what I have, which I suspect is totaly wrong.
"^(\\d+\\.\\d+\\.\\d+\\.\\d+)"
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
Not wrong at all - that's a good start. You've correctly worked out how to use the "\\." and "\\d+" constructs. Nice.
I suggest writing a test to see what sorts of IP strings this matches. Does it work for some sample IPs you expect it to? Does it fail for some sample invalid IP strings that you expect it to fail for?
(I think this pattern probably does exactly what you expect it to - but such tests will be nice to confirm it, and they'll be really useful if you start tweaking the pattern at all, to see if your tweaks have the desired effect or not.)
So, what if the user wants to search for *.112.113.*? Or 123.456.*.*? What pattern strings would be needed then?
I think what you really want here is a general algorithm for transforming a user-input search string like *.112.113.* into a regex string. Then you can use that regex string to check for matching IPs in your set. To do this, you need to first work out what are the rules for transforming a search string into a regex, and then, figure out how to implement this transformation in code. For example, using String's replaceAll() method.
Does that help?
|
 |
Tim Jones
Greenhorn
Joined: Apr 06, 2008
Posts: 28
|
|
Mike Simmons wrote:Not wrong at all - that's a good start. You've correctly worked out how to use the "\\." and "\\d+" constructs. Nice.
I suggest writing a test to see what sorts of IP strings this matches. Does it work for some sample IPs you expect it to? Does it fail for some sample invalid IP strings that you expect it to fail for?
(I think this pattern probably does exactly what you expect it to - but such tests will be nice to confirm it, and they'll be really useful if you start tweaking the pattern at all, to see if your tweaks have the desired effect or not.)
So, what if the user wants to search for *.112.113.*? Or 123.456.*.*? What pattern strings would be needed then?
I think what you really want here is a general algorithm for transforming a user-input search string like *.112.113.* into a regex string. Then you can use that regex string to check for matching IPs in your set. To do this, you need to first work out what are the rules for transforming a search string into a regex, and then, figure out how to implement this transformation in code. For example, using String's replaceAll() method.
Does that help?
Problem solved thanks to your explaination
|
 |
 |
|
|
subject: Wildcard Search on Set
|
|
|