Solved

Regular Expression  Help

Posted on 2011-09-20
24
218 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

10 Experts available now in Live!

Get 1:1 Help Now