4

I have a requirement to scan a folder and filter the files which matches a specific regular expression pattern.

Sample code in the accept method of the FileNameFilter


Scanner fileNameScanner = new Scanner(fileName);

Pattern mainPattern = Pattern.compile(this.pattern);

fileNameScanner.findInLine(mainPattern);

MatchResult result = fileNameScanner.match();

Now, I need a regular expression which filters the file which matches files with names like impl-2.0.xml and at the same time, it should not match impl-Test-2.0.xml.

I tried few settings but does not seem to work. It picks impl-Test-2.0.xml as well.

6
  • 5
    Please be more specific about what you are looking to match. Are you looking for (pseudocode) <string>-<major>.<minor>.xml? If you can describe in words what you are looking for, we can help you write the regular expression. Commented Aug 23, 2012 at 9:24
  • 4
    "I have a requirement" Good, good.. Do you have a question? If so, don't be shy, go ahead and ask it. :) Commented Aug 23, 2012 at 9:31
  • Give limit examples of what the regex should and shouldn't match. Commented Aug 23, 2012 at 9:47
  • Given a set of Strings, i need to have a regular expression which filters strings (files names in my case) of kind impl-2.0 and not impl-Test-4.0. For example, i have the following strings and i need to extract only impl-2.0 and api-3.0 Example Strings: impl-2.0, impl-Test-2.0, api-2.0, api-Test-2.0, api-temp-2.0. What is the regular expression that i should use? Commented Aug 23, 2012 at 10:17
  • 1
    I am happy with <one or more characters of any type>-<single digit>.<single digit>.xml Commented Aug 23, 2012 at 12:29

3 Answers 3

1

See it in action on Regexr:

^\w+-\d\.\d\.xml
Sign up to request clarification or add additional context in comments.

3 Comments

Should be ^\w+-\d\.\d\.xml. Otherwise there could be nothing before the hyphen.
I hereby name this game, Comment Correction Pong! Your turn... :-)
lol :) Your answer is more complete now. If you consider adding a Regexr link, I put pressure on OP to accept it :)
1

I am happy with <one or more characters of any type>-<single digit>.<single digit>.xml

"characters of any type" is a dangerous statement in regular expressions! It is better if we restrict this by assuming you are happy with any letter or number. Based on that assumption, you want:

Pattern p = Pattern.compile("\\w+-\\d\\.\\d\\.xml");

Which will match characters in the range a-z, A-Z, 0-9 followed by a hyphen, one digit, a period, one digit and .xml.

1 Comment

Matches also impl-Test-2.0.xml, api-Test-2.0.xml and api-temp-2.0.xml which are OP doesn't want. OP doesn't want <one or more characters of any type> actually, they wants alphabetical characters I think. Clarification needed.
0

EDIT after comment

For an absolute path with size number limited

Pattern.compile(".*impl-[0-9]{1,2}\\.[0-9]{1,2}.xml");

For name file :

Pattern.compile("impl-[0-9]+\\.[0-9]+.xml");

EDIT 2

final Pattern patt = Pattern.compile("^\\p{Alnum}+\\-[0-9]+\\.[0-9]+.xml$");

Console

impl-3.0.xml
api-3.0.xml
x10-2.0.xml

You can replace ^ by a Java path separator if you grep an absolute path

4 Comments

This will not match some of the other examples provided in the comments. Also, it seems a poor choice of pattern for other reasons. You use {1,} when you could use +. For some reason you've decided the second number can be two digits long, but the first can't? And you accept anything before the impl, when that might not be appropriate.
@DuncanJones is my EDIT seems more appropriate for you?
I have tried these and none of them seem to work Sample Strings impl-3.0.xml, api-3.0.xml, x10-2.0.xml, impl-Test-3.0.xml, api-Test-3.0.xml, 4.0-impl.xml Regular exp Pattern.compile(".+-\\d\\.\\d\\.xml") Pattern.compile("\\w+-\\d\\.\\d\\.xml") Pattern.compile(".*impl-[0-9]{1,2}\\.[0-9]{1,2}.xml") Pattern.compile("impl-[0-9]+\\.[0-9]+.xml") Pattern.compile("\\w-[0-9]+\\.[0-9]+.xml") It should pick only 3 strings "impl-3.0.xml", "api-3.0.xml", "x10-2.0.xml". May the exp. impl-[0-9]+\\.[0-9]+.xml should be generalized.
@ManikandanKannan with you sample I've found a solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.