JoseTlaseca
JoseTlaseca

Reputation: 23

REGEX to match filenname convention

First of all I would like to say thanks to anybody who spend time reading my post.

Well, I am a newbie on the Regex world, I am able to create very basic REGEX but now I want to jump to a more complex one.

The purpose is to match only those filenames wich mets the next naming convention:

The filename should start with a certain char combination, then it should be followed by any 4 numbers, then an underscore. I don't need to take care about the rest of the filename.

There are certain valid combinations for the start of the filename, lets say:

ABCD
HIJK

are the only valid starting strings for the file name.

For example, the next strings are valid:

ABCD9403_XXXXXXXXXXXX.XXX (because it starts with ABCD, then 4 digits and then an underscore) HIJK6701_xXxXxX.xXx (because it starts with HIJK, then 4 digits and then an underscore)

but, the next ones are not valid:

AMCD6987_xxxxxx.xxx (because AMCD does not match any of the valid starting strings above: ABCD or HIJK)

HIJK12F2_xxxxxxx.xxx (It starts with a valid string (HIJK) but the next part does not contain 4 digits (it contains an F instead))

ABCD9547-21654sdasd321.321asd (it matchs one of the valid starting strings and 4 digits but it does not match the underscore.

I am going to code this using Java.

I hope somebody can help me.

All suggestions are well accepted.

Thanks to everybody.

Upvotes: 0

Views: 153

Answers (2)

joescii
joescii

Reputation: 6533

Not suggesting anything is wrong with the previous answers. I thought I would include one that works with comments explaining it.

Pattern p = Pattern.compile(
    "(?:"       + // Open a "non-capturing" group for our two letter sequences
    "ABCD"      + // The sequence ABCD ...
    "|"         + // OR ...
    "HIJK"      + // the sequence HIJK
    ")"         + // close the group
    "\\d"       + // A digit ...
    "{4}"       + // exactly 4 times
    "_"         + // an underscore
    ".*"          // Anything else.
);

assertTrue(p.matcher("ABCD9403_XXXXXXXXXXXX.XXX").matches());
assertTrue(p.matcher("HIJK6701_xXxXxX.xXx").matches());
assertFalse(p.matcher("AMCD6987_xxxxxx.xxx").matches());
assertFalse(p.matcher("HIJK12F2_xxxxxxx.xxx").matches());
assertFalse(p.matcher("ABCD9547-21654sdasd321.321asd").matches());

Upvotes: 0

Darka
Darka

Reputation: 2768

simply like this:

^(ABCD|HIJK)\\d{4}_

Upvotes: 1

Related Questions