Link to home
Start Free TrialLog in
Avatar of ScarletBlue
ScarletBlue

asked on

Implementing a wildcard search in a Vector or string array

hi gurus

i need to implement a wildcard search in a vector and a string array.
If I have a string array with userids:
             String [] userid = {"danG", "thomasG", "davidsK"};

If I do a search on da* then the returned values I want in a string[] or vector
is {"danG", "davidsK"}

how would this work for both vector and string[]
thank-u in advance
SB
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use regular expressions for this. Or for that simple example a substring function would suffice. If you specify the function prototype like this:

public List getMatches(Object o, String toMatch) {
    List source = null;
    if (o instanceof  String[]) {
        source = Arrays.toList(o);
    }
    // now iterate, matching
     
}

you can cope with either Vector or String[]
Avatar of Tols
Tols

It can be done in many ways.

String class offers many methods for finding substrings, comparing etc. Simple create methot for searching strings matching to mask.
Avatar of ScarletBlue

ASKER

Thank u both for your help..thou I am still abit stuck.
Tols I have used your advice and went with String methods.

String [] userid = {"danG", "thomasG", "davidsK"}
String  inputFromUser = "da*"
String[] revisedUsers = null;

for(int i=0; i<userid; i++){
     String x = null;
     if(x.startsWith(inputFromUser)){
         revisedUsers = //HOW DO I ADD THE STRING VALUES TO THIS ARRAY
         response.write(userid[i]);
     }
}

-----------
How do i get rid of the * so that it only searches for strings begining with "da"
and how do I add those values to a string array?




>>How do i get rid of the * so that it only searches for strings begining with "da"

You can't without using regular expressions [unless you just do s.substring("da")], but of course it will then match 'Dada' too.

Using arrays is also not appropriate as arrays are inherently fixed-size, whereas you need a dynamic structure.
Hi,

you can also use regular expressions to match the string. in this case you don't need to get rid of the * character.

The String.match(String regex) method is the same as the Pattern.matches(regex, str) call.


String [] userid = {"danG", "thomasG", "davidsK"};
String inputFromUser = "da*";

Vector revisedUsers = new Vector();

for (int cc = 0; cc < userid.length; cc++)
{
  try
  {
   if (userid[cc].matches(inputFromUser))
      revisedUsers.add(userid[cc]);
   } catch (PatternSyntaxException  ex) { // may be thrown on bad pattern.
      ex.printStackTrace();
   }
}


david
>>but of course it will then match 'Dada' too.

Unless of course you call s.startWith("da") in which case it will not 'armada', but will match 'dad'
If your search string is only searching for prefixies you can rewrite the entered userInput from da* to ^da*. So the string must start with "da" followed by anything .

david
>>you can also use regular expressions to match the string. in this case you don't need to get rid of the * character.

'*' has quite a distinct meaning in terms of regular expressions - and it's not what you probably think it is - as the following demonstrates. It means zero or more of the preceding character in this case. Regular expressions are very powerful if you know how to use them and if you want to do this sort of thing, it's the way to go.


import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Wildcard {

      public static void main(String[] args) {
            String s1 = "drone";
            final String regex = "da*";

            Pattern pat = Pattern.compile(regex);
            Matcher mat = pat.matcher(s1);
            if (mat.find()) {
                  System.out.println("Matcher did find for String " + s1 + " at index " + mat.start());
            }
            else {
                  System.out.println("Matcher did not find for String " + s1);
            }
            // A 'match' operation applies to the whole String
            s1 = "d";
            mat = pat.matcher(s1);
            if (mat.matches()) {
                  System.out.println("Matcher did match String " + s1);
            }
            else {
                  System.out.println("Matcher did not match String " + s1);
            }

      }

}
Hi

For some reason I cannot use java.util.regex.Pattern or java.util.regex.Matcher.
I am using Eclipse developing in a Portal Development environment.

I need to have a jar file of these classes to use it in Eclipse. Where can I find these classes?

Thank-u for everyone's help
SB
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial