0

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?

Edited by newbee_jv: missed a word

2
Contributors
2
Replies
13
Views
9 Months
Discussion Span
Last Post by newbee_jv
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.