0

I know that there are other questions about it, but I can't find the error in my code even with these. I wrote this to check if what is written in my textField is a path but it seems to be uncorrect. Here the code:

textFieldNewGameUrl.addFocusListener(new FocusAdapter() {   
        @Override
        public void focusLost(FocusEvent arg0) {

            boolean isMatched = (textFieldNewGameUrl.getText()).matches("([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?");

            if(isMatched){          
                labelNewGameFeedback.setText("Ok, the path is correct");
            }
            else{
                labelNewGameFeedback.setText("Strange things have happened : check the path.");
            }

        }
    });
Imho42
  • 1
  • 1
  • 1
    What is expected input and behaviour? – Leos Literak Mar 30 '14 at 10:33
  • 1
    What's 'uncorrect' means? – locoyou Mar 30 '14 at 10:36
  • So you want to check a windows path for correctness. In your opinion, is a path consisting of just "NUL" valid? See also [this question / set of answers](http://stackoverflow.com/questions/468789/is-there-a-way-in-java-to-determine-if-a-path-is-valid-without-attempting-to-cre). In other words; just try and open the file and see what happens. – Maarten Bodewes Mar 30 '14 at 10:42
  • Don't forget that `.matches()` tries and checks whether the _whole input_ is matched by the regex unlike what its name seems to imply. Moreover, your regex is quite the piece, you should create a `Pattern` for it. – fge Mar 30 '14 at 11:02
  • Right, sorry: I choose a dir with JFileChooser and I need to see if the path chosen have the right format. I could set the textField uneditable, but i prefer to do not. The output with this code is always "Strange things have happened : check the path." beacause isMarched is always false. I changed the code creating a pattern, i use the Pattern.compile(..) method and matcher m.. but m.matches() is still false. I update the code in the question – Imho42 Mar 30 '14 at 12:04
  • Can you post some examples of the format of dir you're looking to match? – Liquidpie Mar 30 '14 at 19:36
  • Solved ;) The problem was in the regex that didn't allow the " " char Thanks – Imho42 Mar 31 '14 at 07:00
  • What relevant questions have you found, and why didn't they help you? – John Dvorak Mar 31 '14 at 07:00
  • Please post the solution as an answer, not as an edit to the question. – John Dvorak Mar 31 '14 at 07:10
  • Solved: I write this method that do what i want with the regex that works. Thanks to everyone! `public static String checkUrlField(JTextField fieldToCheck){ if((fieldToCheck.getText()).matches("([a-zA-Z]:)?(\\\\[a-zA-Z0-9 .-]+)+\\\\?")){ return ("Ok, the path is correct"); } else{ return ("Strange things have happened : check the path."); } }` – Imho42 Mar 31 '14 at 07:37

1 Answers1

0

Writing a Regex to Check Paths is pretty complicated.

public static void main(String[] args) {
   Pattern pattern = Pattern.compile("([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?");
   System.out.println(pattern.matcher("D:\\").matches());
   System.out.println(pattern.matcher("D:\\A").matches());
   System.out.println(pattern.matcher("A\\B").matches());
   System.out.println(pattern.matcher("\\A\\B").matches());
}

this will output

false 
true 
false 
true

Your pattern is not completely broken, but you require it to start with a backslash. This isn't required for relative paths.

Portree Kid
  • 285
  • 3
  • 11