Solved

Pattern Matching in Java

Posted on 2001-08-20
9
640 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 65
maven example error 3 35
Problem to start Neon 20 53
Java DateChooser? 2 11
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

706 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

16 Experts available now in Live!

Get 1:1 Help Now