0

Here i want to validate filename using regex in java. i implemented below code but this is not works for me for 3rd type file.

Can i check prefix and extenstion in regex ???

My validate filename looks like these 3 ways

1) prefix_digit.digit.extenstion         example : AB_1.1.fuij (Here fuij is my extension)
2) prefix_digit.digit.digit.extenstion   example : AB_1.1.1.fuij
3) prefix_digit.digit.B/P.digit.extensionexample : AB_1.1.B.1.fuij 

Only these 3 types of file valid. 3rd one is beta and pilot version files. if beta and pilot version file is there than is should be like this which i mentioned above

I am going to write some valid and invalid files

**Valid :** 
    AB_1.1.fuij
    AB_1.4.fuij
    AB_1.1.1.fuij
    AB_1.1.B.1.fuij
    AB_3.4.P.7.fuij

***Invalid :***
    AB_0.1.fuij
    AB_1.B.1.1.fuij(B/P should be place on 3rd always)
    AB_1.2.B.0.fuij

CODE :

import java.util.ArrayList;
import java.util.regex.Pattern;

public class democlass {
    /**
     * Test harness.
     */
    public static void main(String[] args) {
        ArrayList<String> demoversion = new ArrayList<String>();


        System.out.println("Result >>>>>>>>>>>>  "
                +isFileValid("AB_1.1.fuij"));
        System.out.println("Result >>>>>>>>>>>>  "
                +isFileValid("AB_1.B.fuij"));
        System.out.println("Result >>>>>>>>>>>>  "
                +isFileValid("AB_1.1.1.fuij"));
        System.out.println("Result >>>>>>>>>>>>  "
                +isFileValid("AB_1.P.1.1.fuij"));
        System.out.println("Result >>>>>>>>>>>>  "
                +isFileValid("AB_1.1.B.1.fuij"));

    }

    private static boolean isFileValid(String input)
    {
        String regexFinalBugFix = "^\\d+\\.\\d+\\.\\d+$"; 
        String regexFinal = "^\\d+\\.\\d+$"; 
        String regexBetaPilot = "^\\d+\\.\\d+\\.\\[BP]+\\.\\d+$"; 
        final Pattern pattern1 = Pattern.compile(regexFinal);
        final Pattern pattern2 = Pattern.compile(regexBetaPilot);
        final Pattern pattern3 = Pattern.compile(regexFinalBugFix);

        String inputVersion = null;

        int suffixIndex = input.lastIndexOf(".");
        int prefixIndex = input.lastIndexOf("_");
        if (suffixIndex > 0 && prefixIndex > 0) {
            inputVersion = input.substring(prefixIndex + 1,
                    suffixIndex);
            String prefixString1 = input.substring(0, 3);
            String suffixString1 = input.substring(suffixIndex);
            if(prefixString1.equals("AB_") && suffixString1.equals(".fuij"))
            {
                if (pattern1.matcher(inputVersion).matches()
                        || pattern2.matcher(inputVersion).matches()
                        || pattern3.matcher(inputVersion).matches()) {
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }
}

OUTPUT :

Result >>>>>>>>>>>>  true
Result >>>>>>>>>>>>  false
Result >>>>>>>>>>>>  true
Result >>>>>>>>>>>>  false
Result >>>>>>>>>>>>  false : It should be valid but it is false, why??
4
  • 2
    You should remove all the unnecessary code that is not required for us to understand and answer the question. Something like 'here are my input strings, here is my regex, and here is what I am expecting' would be enough. Commented Feb 4, 2013 at 18:39
  • Why are you explicitly pulling the prefix and suffix off? That can easily be done in the regex and if you need those string values you can do that with captures. For that matter, why three separate regex strings? You can capture all three forms with prefix and suffix in a single regex. Commented Feb 4, 2013 at 18:47
  • Figured I'd give a quick first attempt at a single regex to deal with prefixes and suffixes and all three forms. I admittedly haven't tested this, but I think it is at least close. [^_]+_\\d+\\.\\d+\\.([^.]+|\\d+\\.[^.]+|[BP]\\.\\d+\\.[^.]+) Commented Feb 4, 2013 at 18:56
  • @Mike thanks for your answer but how can i add my prefix and suffix with regex? Can u please help me to figure out this? Commented Feb 4, 2013 at 19:25

4 Answers 4

2

Your regexBetaPilot is wrong: you are escaping the opening bracket of the [BP] class. Try this instead:

String regexBetaPilot = "^\\d+\\.\\d+\\.[BP]+\\.\\d+$";

You can easily combine all three patterns into a single pattern:

String regex = "\\d+\\.(\\d+\\.([BP]+\\.)?)?\\d+";

You don't need the anchors (^ and $). Since you are using matches() instead of find(), it will always try to match the entire string.

EDIT I left in the + after [BP] because that's what you had in your original code. However, if you want to match a single B or P, then you should remove the + from the pattern.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks For Your reply,Here can i add my suffix and prefix with regex itself? and what u did in regex strin where 3 regex are combine? here can u please explain me about () and ? symbol meaning? Please, thanks
@Sam_k - To match literal values, just add them to the regex literally. So, for instance, the regex could be "AB_\\d+\\.(\\d+\\.([BP]+\\.)?)?\\d+\\.fuij". The construct (X)` simply means to match the pattern X as a separate match group. The construct X? means that X can appear 0 or 1 time (it's the same as X{0,1}). Together, (X)? means that the pattern inside the parentheses is optional. (It's more or less the same as X? except it allows X to be more than a single item.) See the docs for Pattern.
0

You are escaping the opening bracket of [BP], so it tries to find a [ in the string.

This works:

String regexBetaPilot = "^\\d+\\.\\d+\\.[BP]+\\.\\d+$"; 

Comments

0

Something like this should work with AB being static:

Regular Expression: AB_\d+\.\d+((\.\d){0,1}|\.[BP]\.\d+)\.fuij

as a Java string AB_\\d+\\.\\d+((\\.\\d){0,1}|\\.[BP]\\.\\d+)\\.fuij

This misses two of your listed invalids, but I was unsure why they should be invalid. I can halep more if you explain the rules for success / failure better?

4 Comments

thanks for your kind response? What you wrote about you explain the rules for success / failure better? Means?
Is AB always there? Why should AB_0.1.fuij fail and AB_1.1.fuij succeed in your example?
Matthew, O is never been stands for version, What is the meaning of 0 there
I am just pulling those from your list above.
0

You can simplify your regular expression to

AB_\d+\.\d+(?:(?:\.[BP])?\.\d+)?\.fuij

This matches AB_digits.digits. Then comes an optional .digits, .B.digits or .P.digits. And finally matches .fuij. From your examples, there might be only a single B or P. If you wish to match multiple Bs and Ps, just add the + again.

And then your isFileValid() function might be reduced to

private static boolean isFileValid(String input)
{
    final String re = "AB_\\d+\\.\\d+(?:(?:\\.[BP])?\\.\\d+)?\\.fuij";
    final Pattern pattern = Pattern.compile(re);
    return pattern.matcher(input).matches();
}

Comments

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.