Solved

Pattern Matching in Java

Posted on 2001-08-20
9
654 Views
Last Modified: 2007-12-19
I want to make a function of signature like below
  "private boolean funcPat(File f1, String pattern)"
in which I'll pass file like C:\myfile.java or C:\myfile.exe or may be anything, alongwith pattern.
pattern may be any regular expresion like "m*.e*". The function will match the pattern with filename and its extention and will return true or false.

examples:
Say File  = myfile.java .
if pattern = myf*.ja*  return true.
if pattern = myf*.exe  return false.
if pattern = fyf*.ja*  return false.
if pattern = *yf*.*  return true.

if anybody knows how can it be done in Java pls let me know.

0
Comment
Question by:rajeevtyagi
9 Comments
 

Accepted Solution

by:
raghu_k_n earned 20 total points
ID: 6408751
Hello Rajiv,
     I think u can do this simply by using StringTokenizer in the Util package.
Tokenize ur Pattern on the delimeter '*'.
For each token in the sequence, call this
     if(!fileName.indexOf(token)==-1)
If for each token, this is true then return true else false.

HTH,
Raghu
0
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 6408759
0
 
LVL 6

Assisted Solution

by:kotan
kotan earned 20 total points
ID: 6408781
Below is the code I have written for you. You can test it.

public boolean funcPat(File f1, String pattern) {
        boolean exist = true;
        String fname = f1.getName();
        int fnameIndex = 0;
       
        java.util.StringTokenizer st = new java.util.StringTokenizer(pattern, "*");
       
        while (st.hasMoreTokens()) {
            String token = st.nextToken();

            fnameIndex = fname.indexOf(token, fnameIndex);

            if (fnameIndex == -1) {
                exist = false;
                break;
            }
           
            fnameIndex += token.length();
        }
       
        return exist;
    }

Best Regards,
kotan
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 6

Assisted Solution

by:gadio
gadio earned 20 total points
ID: 6408843
Whenever pattern matching is needed - why not go for the real power? Use regular expressions. Download the package gnu.regexp at http://www.cacas.org/java/gnu/regexp/
With regular expressions you can match much more than what you need:

import gnu.regexp;

   public static boolean funcPat(File f1, String patt) {
     REMatch res;
     try {
       RE expr = new RE(patt, RE.REG_ICASE);
       = expr.getMatch(f1);
     } catch(Exception e){
        e.printStackTrace();
     }
     return (res != null);
   }      


This way, you use the match patterns:
"
say File = myfile.java pattern ="my.*\.J.*" return=true;
say File = myfile.java pattern ="Ry.*\.J.*" return=false;
say File = myfile.java pattern ="my.*\.E.*" return=false;
say File = myfile.java pattern ="my.*\.J.*r" return=false;
say File = myfile.java pattern =".*e\..*a" return=true;
"
because '.*' is equvalent to what you used '*' and '\.' to '.'
But you can also do more powerfull matches like:
say File = myfile.java pattern ="my..le.j.{3}" return=true;
which means - 'my', followed by two chars, 'le.j', and three chars after and so on.


Regards,
gadio.

0
 
LVL 2

Assisted Solution

by:Andrey_Kulik
Andrey_Kulik earned 20 total points
ID: 6410417
This algorithm uses recurrent expression

/**
 * Function match pattern for string (dynamic method)
 * use '*' pattern for any symbols
 * use '?' pattern for any single symbol
 * Example: check("*a?bc*bb?", "abcaabcbbb") returns true;
 * @return boolean
 * @param pattern java.lang.String
 * @param string java.lang.String
 */
public static boolean check(String pattern, String string) throws IllegalArgumentException {
     if (pattern == null || string == null)
          throw new IllegalArgumentException("Pattern or string cannot be a null");
         
     int pLength = pattern.length() + 1;
     int sLength = string.length() + 1;
     
     int[][] m = new int[pLength][sLength];

     // initialize variables
     int highPosition, tempPosition = 0;
     m[0][0] = 1;
     // iterate pattern
     for (int i = 0; i < pLength-1; i++)
     {
          highPosition = tempPosition;
          switch (pattern.charAt(i))
          {
               case '*':
                    m[i+1][highPosition] = m[i][highPosition];
                    for (int j = highPosition+1; j < sLength; j++)
                         m[i+1][j] = m[i+1][j-1] + 1;
               break;
               case '?':
                    for (int j = sLength-2; j >= highPosition; j--)
                         if (m[i][j] > highPosition)
                         {
                              m[i+1][j+1] = m[i][j]+1;
                              tempPosition = j+1;
                         }
               break;
               default:
                    for (int j = sLength-2; j >= highPosition; j--)
                         if (pattern.charAt(i) == string.charAt(j) && m[i][j] > highPosition)
                         {
                              m[i+1][j+1] = m[i][j]+1;
                              tempPosition = j+1;
                         }
          }
     }

     return m[pLength-1][sLength-1] == sLength;
}

Best regards
Andrey
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6410443
Duplicate question?

If you don't like the gnu implementation, you might consider other regular expression packages, two at apache and one from IBM that I know of:

http://jakarta.apache.org/oro/index.html
http://jakarta.apache.org/regexp/index.html
http://www.alphaworks.ibm.com/tech/regex4j

I've used the one from IBM but didn't consider it's memory usage. It seems to be reasonably fast, however, and easy to use. And in the end, completing the job using a regex package was substantially faster development time and much less custom code than if I had tried to write the equivalent myself. And less code almost always translates to fewer defects and simplified maintenance.

Best regards,
Jim Cakalic
0
 
LVL 6

Expert Comment

by:gadio
ID: 6410473
"And in the end, completing the job using a regex package was substantially faster development
time and much less custom code than if I had tried to write the equivalent myself. And less code almost
always translates to fewer defects and simplified maintenance.
"

True.

:-)
0
 

Expert Comment

by:CleanupPing
ID: 9061825
rajeevtyagi:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9332817
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between raghu_k_n, objects, kotan, gadio and Andrey_Kulik

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 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