[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking for a simple pattern in a String

Posted on 2004-09-04
9
Medium Priority
?
246 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

656 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