Solved

Regular Expression  Help

Posted on 2011-09-20
24
221 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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