0

I have a string of "abc123(" and want to check if contains one or more chars that are not a number or character.

"abc123(".matches("[^a-zA-Z0-9]+"); should return true in this case? But it dose not! Whats wrong?

My test script:

public class NewClass {
    public static void main(String[] args) {
        if ("abc123(".matches("[^a-zA-Z0-9]+")) {
            System.out.println("true");
        }
    }
}
6
  • first things first: you do not need the + at the end Commented Jul 13, 2012 at 13:52
  • On a point of purely REGEX grammar, it seems fine (I don't know Java, though.) Returns true in JavaScript. Definitely returns false? Commented Jul 13, 2012 at 13:54
  • Jup. Going to add my test script. Commented Jul 13, 2012 at 13:54
  • matches, in Java, means "matches the whole string". In your case, the '(' at the end is the only part matching "a sequence of one-or-more chars that is not alphanumeric"... Commented Jul 13, 2012 at 14:00
  • Yup.. matches(yourRegex) in Java really means matches("^" + yourRegex + "$") Commented Jul 13, 2012 at 14:12

3 Answers 3

5

In Java, the expressions has to match the entire string, not just part of it.

myString.matches("regex") returns true or false depending whether the string can be matched entirely by the regular expression. It is important to remember that String.matches() only returns true if the entire string can be matched. In other words: "regex" is applied as if you had written "^regex$" with start and end of string anchors. Source

Your expression is looking for part of the string, not the whole thing. You can change your expression to .*YOUR_EXPRESSION.* and it will expand to match the entire string.

Sign up to request clarification or add additional context in comments.

4 Comments

And how can I fix this? I want to now if it contains it, regardless of the rest!
@wowpatrick: You can add .* to the start and end of you existing expressions.
Hey yeah that work! Could you add it to the answer? Than were don here :)
Great, thanks! Works fine now and got a better understanding of Javas regex.
4

Rather than checking to see if it contains only letters and numbers, why not check to see if it contains anything other than that? You can use the not word group (\W) and if that returns true than you know the string contains something other than the characters you are looking for,

"abc123(".matches("[\W]");

If this returns true than there is something other than just word characters and digits.

Comments

2

Expression [^A-Za-z0-9]+ means 'not letters or digits'. You probably want to replace it with ^[A-Za-z0-9]+$ which means 'Only letters or digits'.

1 Comment

That regex you're proposing will match one character string only. Add a +: ^[A-Za-z0-9]+$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.