Solved

Checking for a simple pattern in a String

Posted on 2004-09-04
9
235 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 102
Custom Java Application connects to database: Which one of the 20? 4 41
Convert from a json string array to a Java object 3 64
JDBC performance 8 29
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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