0

I am trying to test if my String contains a character ( a-z) (A-Z) and a number (0-9)

package testing;

import java.io.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;


public class Testing extends JPanel 
{

    public static void main(String[] args) 
    {
        String foo = "abc1";

        if (foo.matches(".*[0-9].*") && foo.matches(".*[A-Z].*") && foo.matches(".*[a-z].*"))
        {
            //contain letters and numbers only
            System.out.println("Foo");
        }


    }


}

I am expecting Foo to be printed out but there seems to be a problem with my regex expression. Can someone help me out ??

Thanks

2
  • 1
    your foo doesn't match foo.matches(".*[A-Z].*") Commented May 7, 2014 at 3:43
  • Do the characters have to appear before the number? Commented May 7, 2014 at 3:48

6 Answers 6

2

change

foo.matches(".*[0-9].*") && foo.matches(".*[A-Z].*") && foo.matches(".*[a-z].*")

to

foo.matches(".*[0-9].*") && (foo.matches(".*[A-Z].*") || foo.matches(".*[a-z].*"))
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the .* before and after the [] since the expression is not being anchored at the start or end of the source string.
@Jason - Yes he does need them. matches matches the entire expression, not a substring.
1

Your problem is that foo.matches(".*[A-Z].*") will return false, as there are no capital letters in foo = "abc1".

Comments

1
foo.matches(".*([0-9]+).*") && foo.matches(".*([a-zA-Z]+).*")

Comments

0

The following will identify a match in one regex:

foo.matches(".*([0-9].*[a-zA-Z])|([a-zA-Z].*[0-9]).*")

5 Comments

No, it matches strings that have a letter anywhere before a number or a number anywhere before a letter. I didn't use ^ or $ so it is not anchored to the start or end of the source string.
Why .*? (.*) itself means 0 or more, no need of ?
@TejasPatel ? means reluctant grab. In this case it isn't required.
I am unclear for the fact that he just wants to match a-z A-Z 0-9
@hwnd Read the first line of the question. "I am trying to test if my String contains a character (a-z) (A-Z) and a number (0-9)"
0

You can use a single expression instead of multiple as follows -

if (foo.matches("(?=.*[a-zA-Z])(?=.*[0-9]).*")) {
    //...

That will make sure that foo contains at least one of the alphabets (lower or capital) and at least of the digits.

4 Comments

What's the performance of this expression like? I have a suspicion it might run very slowly on large strings.
How large strings are we talking about?
I don't know. It was just a question. I've seen regular expressions similar to this one running into problems, when the string being matched was only a dozen characters or so. It would be interesting to test this.
Seems like myregextester.com is a good online tool for the purpose. You can test the pattern ^(?=.*[a-zA-Z])(?=.*[0-9]).*$ there with different inputs of your choice and see the results.
-1

Try the following Regex for Alpha Numeric...

foo.matches("\\w.*")

Or

foo.matches("\\w+");

1 Comment

" ... contains a character AND a number ... "

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.