4

I use regex

r="[^A-Za-z0-9]+"; 

to detect if a string has one or more chars other than letters and digits;

Then I tried the following:

Pattern.compile(r).matcher(p).find();

I tested:

! @ # $ % ^ & * ( ) +  =- [ ] \ ' ; , . / { } | " : < > ? ~ _ `

Most of the time, it works except backsplash \ and caret ^.

e.g.

String p = "abcAsd10^"    (return false)
String p = "abcAsd10\\"   (return false) 

Anything I miss?

5
  • I get matches for "abcAsd10\\" and "abcAsd10^", so here it returns true. Note that I use C# so I think it has to to with Java. It can help if you write a little code sample that demonstrates your problem. Commented Jun 5, 2012 at 18:47
  • FWIW, both strings appear to match on Ideone. Commented Jun 5, 2012 at 18:50
  • Sorry, I had r="[^A-za-z0-9]+"; (with little "z"). That causes returning "false" Commented Jun 5, 2012 at 18:57
  • So you have your answers now? Not clear if you had a type in your so question or the problem itself. Commented Jun 5, 2012 at 18:58
  • I had r="[^A-za-z0-9]+"; in my code (with little "z", i.e. A-z). That causes returning "false". But why it works with other special chars except backsplash \ and caret ^? Commented Jun 5, 2012 at 19:10

7 Answers 7

2

The following code prints "Found: true" when I compile and run it:

class T
{
    public static void main (String [] args)
    {
        String thePattern = "[^A-Za-z0-9]+"; 
        String theInput = "abskKel35^";
        boolean isFound = Pattern.compile(thePattern).matcher(theInput).find();
        System.out.println("Found: " + isFound);
    }
}

Not sure why you would see a different result...

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

Comments

1

Shouldn't that be:

r="[^A-Za-z0-9]+"; 

In your question you write a_z (underscore)

Comments

1

When I run this code on my machine it prints true..

        String r="[^A-Za-z0-9]+"; 
        String p = "abcAsd10\\" ;
        Matcher m = Pattern.compile(r).matcher(p);
        System.out.println(m.find());

true

3 Comments

Originally he had [^A-Za_z0-9]+
so we need to flag this question... it is nothing but a bug
The original one ([^A-Za_z0-9]+) should give true, Paul. Aynone try '[^A-za_z0-9]+'?
1

You could also change only:

[\w&&^_]+

Where:

  • \w A word character with underscore: [a-zA-Z_0-9]
  • \W A non-word character: [^\w]

See more in class Pattern

1 Comment

Perhaps it might be interesting to experience another way.
0

Try quoting your input into the matcher.

Pattern.quote(p)

Comments

0

Simply do:

p.matches(".*\\W.*"); //return true if contains one or more special character

Comments

0

Sorry, I had

r="[^A-za-z0-9]+"; (with little "z", i.e. A-z). 

That causes returning "false". But why it works with other special chars except backsplash \ and caret ^?

Comments

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.