termin8tor 0 Newbie Poster

Hello all! I am working on a regular expression to be used in descrambling words in my scrabble solver.

What I am attempting to do is write a regular expression that can be used to find all characters within a scrambled word, irrespective of order and match them to a word.

public static void main(String[] args) {
        // TODO code application logic here
        String regex = "(?=.*c)(?=.*a)(?=.*n)(?=.*a)(?=.*d)(?=.*a).*$";
        Pattern patternObj = Pattern.compile(regex);
        Matcher matcher = patternObj.matcher("canade");
        if(matcher.matches()){
            System.out.println("Pattern matches.");
        }
        else
            System.out.println("Pattern does not match.");
    }

The example I show above demonstrates the regular expression, the problem is however that when I check to see if the pattern matches the word canade, it does as it contains all of the base word (canada) components. The problem being this will produce a match as there in an 'e' in it, whereas there is no 'e' in the regular expression. I was wondering how I would re-write this regex to exclude any characters outside of its scope.

I'm still new to regular expressions and Ii realise that normally, they aren't used in the particular manner I am attempting to use them.

Anyway any help is appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.