Results 1 to 1 of 1
Thread: Please Help - Regex
- 04-28-2010, 05:28 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Please Help - Regex
Hello,
I have a few strings with the patterns : "1234AB", "1234ABC", "AB1234CDE" which I am trying to match. For starters, i don't know if I have the regular expressions correct. When I compile, I get some of the desired results but I also get some duplicates. Can someone please help me figure out the proper regular expression?.
Also, I was trying to match more than one pattern and I am not doing too well with that either.
I would really appreciate some help....Please.
Thanks.
Java Code:import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { public static void main( String args[] ) { // create regular expression Pattern ex1 = Pattern.compile( "[A-Z][A-Z]\\d{4,5}[A-Z][A-Z]+" ); Pattern ex2 = Pattern.compile( "\\d{4,5}[A-Z][A-Z][A-Z]+" ); Pattern ex3 = Pattern.compile( "\\d{4,5}[A-Z][A-Z][A-Z]\\d+" ); String string1 = "1234ABC\n" + "1234AB\n" + "1234AB1\n" + "1234a\n" + "AB1234CDE\n"; // match regular expression to string and print matches Matcher matcher = (ex1.matcher( string1 )); Matcher matcher2 = (ex2.matcher( string1)); while ( (matcher.find())||(matcher2.find())) System.out.println( matcher.group() ); System.out.println( matcher2.group() ); } // end main } // end class RegexMatches
Similar Threads
-
Regex for the pattern
By mallikarjun_sg in forum Advanced JavaReplies: 12Last Post: 05-06-2010, 12:16 PM -
Help with regex
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 03-29-2010, 12:40 AM -
Using regex
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 03-28-2010, 11:07 PM -
Interpretation of regex?
By Ms.Ranjan in forum New To JavaReplies: 2Last Post: 04-23-2009, 06:37 PM -
Multiline Regex
By zriggle in forum New To JavaReplies: 1Last Post: 02-26-2009, 04:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks