Solved

Checking for a simple pattern in a String

Posted on 2004-09-04
9
208 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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11979586
May be I am late but pattern matching is best for such operations :)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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:

746 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

12 Experts available now in Live!

Get 1:1 Help Now