?
Solved

Checking for a simple pattern in a String

Posted on 2004-09-04
9
Medium Priority
?
242 Views
Last Modified: 2010-03-31
I am writing a text parser and I want to be able to detect extracts of text that are in the following pattern

XX1234

e.g 2 Capital Letters, followed by 4 numbers

What would be the best way to do this?

Cheers

0
Comment
Question by:chocobogo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 3

Accepted Solution

by:
mte01 earned 400 total points
ID: 11979556
The following code will do the trick (although it is better to use Lex & Yacc parsing utilities:


   String teststring = new String("Anything that has AA1234 or CF3654");
    String[] foundstring = new String[10];
    int found = 0,j=0;
    for(int i=0;i<=teststring.length();i++)
    {
        if(found < 2)
        {
          if(teststring.charAt(i) >= 'A' && teststring.charAt(i) <= 'Z')
            found++;
          else
          {
            found = 0;
            continue;
          }
        }
        else if(found >= 2 && found < 6)
        {
          if(teststring.charAt(i) >= '0' && teststring.charAt(i) <= '9')
            found++;
          else
          {
            found = 0;
            continue;
          }
        }
        else if(found==6)
        {
          foundstring[j++] = teststring.substring(i-6,i);
          found=0;
        }
      }
      for(int i=0;i<j;i++)
      {
        System.out.println(foundstring[i]);
      }

0
 

Author Comment

by:chocobogo
ID: 11979582
Cheers dude!
I will work with the code above & look into  Lex & Yacc for the future!

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11979583
Probably this will help you



import java.util.*;
class  SearchText
{
      public static void main(String[] args)
      {
               String str="AA12442AA678889AB67678AA3333AZ6767YT7878FG4559";

               Vector vect=new Vector();
               for(int i=0;i<str.length();i++)
            {
                        try
                        {
                              String str1=str.substring(i,i+2);
                              String str2=str.substring(i+2,i+6);

                              if(str1.length()==2 && str2.length()==4)
                              {

                                       if(str1.matches("[A-Z]*") && str2.matches("[0-9]*"))
                                          {

                                             vect.add(str1+str2);
                                          }
                              }
                        }
                        catch(Exception ex)
                        {
                              
                        }
            }
            for(int i=0;i<vect.size();i+=1)
            {
                  System.err.println(vect.get(i));
            }

      }
}

Regards
Sudhakar
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11979586
May be I am late but pattern matching is best for such operations :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11979589
Regular expressions can help

System.out.println( "sef<DE1245>6fq<PF0047>sfgsfd".
                            replaceAll("([ABCDEFGHIJKLMNOPQRSTUVWXYZ]){2}([1234567890]){4}", "######" ) );

This prints:

sef<######>6fq<######>sfgsfd

Now in the resulting string you search for all occurrences of "######" with indexOf().
The indices returned by indexOf are the ones that can be used in the original string.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11979590
>> String[] foundstring = new String[10];

Take care about this

 String[] foundstring = new String[str.length()/6]; may save you  where str is ur main string
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11979591
Whoops accepted already...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11979599
String[] foundstring = new String[str.length()/6+1]; may save you  where str is ur main string
0
 

Author Comment

by:chocobogo
ID: 11980186
cheers for all the extra comments guys !!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

743 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