Solved

Pattern Matching in Java

Posted on 2001-08-20
9
676 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
[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
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
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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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

630 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