Solved

Regular Expression  Help

Posted on 2011-09-20
24
222 Views
Last Modified: 2012-08-14
Hi

Looking for a regExp that matches on any number of uppercase alphas followed by any number of digits OR just the uppercase alphas

ADOG //good
ADOG1 // good
ADOG11 //good

ADOG1A //bad
1ADOG //bad
AD1G //bad

thanks
0
Comment
Question by:Molko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 5
  • 4
24 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36569138
I think this should match:

[A-Z0-9]+
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569143
sorry didn't understand
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569149
this should match
[A-Z]+[0-9]*$
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 47

Expert Comment

by:for_yan
ID: 36569177
No, it actually matches too much
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569202
       Pattern p10 =  Pattern.compile("^[A-Z]+[0-9]*\\z");
        Matcher m10 = p10.matcher("ADOG11");
        if (m10.find())System.out.println("match");
          else  System.out.println("no match");
           m10 = p10.matcher("ADOG1A");


          if(m10.find())System.out.println("match");
        else  System.out.println("no match");

Open in new window


match
no match

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569205
The above I already tested
0
 

Author Comment

by:Molko
ID: 36569316
You think  "^[A-Z]+[0-9]*\\z" is good ? what does the \\z mean ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569326
\\z means the end of string
and ^ means the begiining of it
0
 

Author Comment

by:Molko
ID: 36569350
I thought $ meant end of string ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569356
ADOG  matched
ADOG1  matched
ADOG11  matched
ADOG1A not   matched
1ADOG not   matched
AD1G not   matched

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569364
Yes, it is another variant, I think it would work with $ also
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569376
without these beginning and end
ADOG1A  is matching, because it is the last "A" which is matching
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569384
This is testing all of your examples
        String [] toTest = {
                "ADOG",
                "ADOG1", // good
                "ADOG11", //good

                "ADOG1A", //bad
                "1ADOG", //bad
                "AD1G" //bad

        };

       Pattern p10 =  Pattern.compile("^[A-Z]+[0-9]*\\z");

        for(String st: toTest){
            Matcher mt = p10.matcher(st);

            if(mt.find()){
                System.out.println(st+ "  matched");


            }
            else
              System.out.println(st+ " not   matched");
        }

Open in new window


The output see above
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569588
   String [] toTest = {
                "ADOG",
                "ADOG1", // good
                "ADOG11", //good

                "ADOG1A", //bad
                "1ADOG", //bad
                "AD1G" //bad

        };

       Pattern p10 =  Pattern.compile("^[A-Z]+[0-9]*$");

        for(String st: toTest){
            Matcher mt = p10.matcher(st);

            if(mt.find()){
                System.out.println(st+ "  matched");


            }
            else
              System.out.println(st+ " not   matched");
        }

Open in new window


Output
ADOG  matched
ADOG1  matched
ADOG11  matched
ADOG1A not   matched
1ADOG not   matched
AD1G not   matched

Open in new window


without $ or \z in the end of regex, though, ADOG1A will also be matching
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36569761
>>... on any number of uppercase alphas

'Any' would of course include zero - is that your intention?
0
 

Author Comment

by:Molko
ID: 36570957
Hi,

'any' as in more than zero. There should be at least one alpha
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36570960
That's how I understood it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36572204
>>any' as in more than zero. There should be at least one alpha

In that case, all you need is
boolean valid = input.matches("[A-Z]+\\d*");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36572424

@CEHJ,

Of course you can use \d instedd of [0-9] - these are equivalent,
but you need to put ^ and $ at the start and the end, as otherwise it will match all
six of the examples above

           String [] toTest = {
                "ADOG",
                "ADOG1", // good
                "ADOG11", //good

                "ADOG1A", //bad
                "1ADOG", //bad
                "AD1G" //bad

        };

       //Pattern p10 =  Pattern.compile("^[A-Z]+[0-9]*$");

        Pattern p10 =  Pattern.compile("[A-Z]+\\d*");

        for(String st: toTest){
            Matcher mt = p10.matcher(st);

            if(mt.find()){
                System.out.println(st+ "  matched");


            }
            else
              System.out.println(st+ " not   matched");
        }

Open in new window


ADOG  matched
ADOG1  matched
ADOG11  matched
ADOG1A  matched
1ADOG  matched
AD1G  matched

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 36572646
>>as otherwise it will match all six of the examples above

That's incorrect. All you need is the below

public class Test {
    public static void main(String[] args) {
	String [] toTest = {
	    "ADOG",
	    "ADOG1", // good
	    "ADOG11", //good

	    "ADOG1A", //bad
	    "1ADOG", //bad
	    "AD1G" //bad

	};
	final String PATTERN = "[A-Z]+\\d*";
	for(String st: toTest){
	    System.out.printf("%s valid? %b\n", st, st.matches(PATTERN));
	}
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36572687
well, with ^ and $ it works with all variants
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36575179
>>it works with all variants

So does the simpler code i posted
0
 

Author Closing Comment

by:Molko
ID: 36575327
Cheers
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36575461
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question