Solved

Regular Expression  Help

Posted on 2011-09-20
24
220 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 84
xampp tool 12 48
eclipse console opening separately 2 17
Is Applet the way to go for my drag and drop system? 8 10
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

776 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