trying to create a matcher for finding if title is not correct i.e. has to be mr, mrs, miss etc..but this allows mrmrmr etc when it should not. tried different setups to keep failing i.e. [[]] or ().

public void valTitle(){
    //validate title
    while (this.title.matches("^[\\s]+$")
                                    ||this.title.matches("")
                                    ||this.title.matches("[^m]&&[^r]*")
                                    ){
                                System.out.println("Title can not"
                                        + " be empty or have spaces, must contain "
                                        + "a-z characters and has to be correct "
                                        + "length in size i.e. Mr or Miss");
                                System.out.println("Enter your title ?");
                                 title = inputDetails.nextLine();
        }
    }

Recommended Answers

All 4 Replies

You could use (Mr|Mrs|Miss){1} , but really equalsIgnoreCase() could be used just as well for those three specific cases.

thats great but the problem is that i need it not to equal (Mr|Mrs|Miss){1} i.e. with characters [^m] but i dont think != works with this type of regex ?

Just use !myString.matches(...) instead.

thank you very much, i was about to give up.I did not know that could be used :O.

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.