Solved

Checking for a simple pattern in a String

Posted on 2004-09-04
9
223 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 3

Accepted Solution

by:
mte01 earned 100 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 71
diffSum example 4 37
SHA2 certs for IIS AND Java? 2 90
mysql jsp example issue 32 36
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

773 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