Solved

Regular Expression  Help

Posted on 2011-09-20
24
219 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now