Pattern Matching in Java

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.

rajeevtyagiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
raghu_k_nConnect With a Mentor Commented:
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
 
kotanConnect With a Mentor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gadioConnect With a Mentor Commented:
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
 
Andrey_KulikConnect With a Mentor Commented:
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
 
Jim CakalicSenior Developer/ArchitectCommented:
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
 
gadioCommented:
"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
 
CleanupPingCommented:
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
 
VenabiliCommented:
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
All Courses

From novice to tech pro — start learning today.