Hello everyone,

I am new to Regular expression, and would like some hints, please if someone can direct me to right place.

I have a bunch of files in a directory, and i need to rename them based on certain pattern. I think I need to use regular expression to parse out file name and see if the file name matches my pattern and then change the file name accordingly.

Here's the scenario.

Original File Name: 220-X F-30300-8701_MAR__000_v00_TIL.pdf

The pattern I am looking for is this: ###_v##_TIL.pdf
3 consequetive digit (numbers), underscore, litter v (upper case or lower), ## (2 digits), literal _TIL.pdf

    String str = "220-X F-30300-8701_MAR_001_v00_TIL.pdf";
    String reg = "^\\d{3}_v\\d{2}_TIL.pdf";

    Pattern regex = Pattern.compile(reg, Pattern.COMMENTS);
    Matcher regexMatcher = regex.matcher(str);
    boolean result = regexMatcher.matches();
    System.out.println("Result = " + result);

    output
    --------
    Result = false

It appears that it can't find the match. Am I missing something?

Recommended Answers

All 2 Replies

Hi newbee_jv,

Consider using this:

String reg = ".+?\\d{3}_v\\d{2}_TIL.pdf$";

What I included in your reg expression is .+? that matches one or more character before the expression you wanted.
The $ means match end of line. Without this, the expression should still work though...
Ttry it out....

Hope it helps.

Thank you very much. it worked like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.