?
Solved

File name pattern matching

Posted on 2007-04-04
15
Medium Priority
?
3,411 Views
Last Modified: 2008-01-09
I want to use regular expression to match a file name with a given file name pattern. The pattern can be like "*.log", or "*.log.2*".

For example, I have a file name like this: "xxx.log.2007.04.04"

Then "*.log" will not match this file name.

But "*.log.2*" will match this file name.

How do I construct the regular expression to use the pattern "*.log" and "*.log.2*" then? Thanks.
0
Comment
Question by:integrosys
[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
15 Comments
 
LVL 11

Expert Comment

by:elfe69
ID: 18849181
In that case, I would simply use standard String functions:

if ((filename.endsWith(".log")) || (filename.indexOf(".log.2") > -1)) {
    // the filename matches one of the patterns
}
0
 

Author Comment

by:integrosys
ID: 18849209
In that case, the code would have to search and remove those * from the pattern. This is very troublesome.
0
 

Author Comment

by:integrosys
ID: 18849228
The contents of the following method is what I need:

public boolean match(String fileName, String pattern) {
  // return true if fileName matches pattern; false otherwise
}

So if I call match("xxx.log.2007.04.04", "*.log"), it will return false.

If I call match("xxx.log", "*.log"), it will return true.

If I call match("xxx.log.2007.04.04", "*.log.2*"), it will return true.
0
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

 
LVL 11

Accepted Solution

by:
elfe69 earned 100 total points
ID: 18849271
Use standard Java regex:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

public boolean match(String fileName, String pattern) {
  Pattern p = Pattern.compile(pattern);
  Matcher m = p.matcher(filename);
  return m.matches();
}


0
 

Author Comment

by:integrosys
ID: 18849296
Your solution does not work at all. Have you actually tried running it at all?
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18849338
My solution work if you use regular expression patterns (".*\\.log" instead of "*.log").

If you want other regular expression syntax, I think you have to write your own method.
0
 

Author Comment

by:integrosys
ID: 18849363
You don't seem to get my question. My question is how to write that method using regular expression. If I can write it myself, I wouldn't need to post the question here.
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18849371
What is the expression syntax you want to support ? Only * tokens to replace one or more characters ?
0
 

Author Comment

by:integrosys
ID: 18849422
I need the code for the following method:

public boolean match(String fileName, String pattern) {
  ...
}

So if I call match("xxx.log.2007.04.04", "*.log"), it will return false.

If I call match("xxx.log", "*.log"), it will return true.

If I call match("xxx.log.2007.04.04", "*.log.2*"), it will return true.

NOTE: I do not want to change the pattern that I pass in. This pattern is provided by the users. The users do not understand regular expression. So if they pass in "*.log", then the method itself has to convert to the proper regular expression itself.

Thanks.
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18849633
Use a tokenizer to traverse the pattern and check if you can find all the tokens in the filename in the right order.
0
 

Author Comment

by:integrosys
ID: 18855337
Can you be more specific? Not sure what you mean.
0
 
LVL 4

Assisted Solution

by:funnyveryfunny
funnyveryfunny earned 100 total points
ID: 18994881
String regex = (pattern.equals(*.log))? "^.+\\.log$":"^[\\w]+\\.log[.\\d]+$";
return fileName.matches(regex);

Something like this should do the trick assuming xxx is a character class.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20143927
Forced accept.

Computer101
EE Admin
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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