List the file paths that match the regular expression when both paths and the file name contain a wildcard

Our system allows both the folder and the file to be in regex format. for instance

/dir1*/abcd/efg.? is legal. /dir* could match /dira and also /dirb/cde 

I want to find all files matching this pattern. To eliminate the unnecessary operation, I want the root directory to start the file list and filtering.

Is there an efficient way to get the root directory of a regular expression path template?

A few test cases:

 /abc/def*/bc return /abc /abc/def* return /abc /ab.c/def* return / /ab\.c/def* return /ab.c 
+4
source share
2 answers

Edited: Added relative path processing

 String path; String root = path.replaceAll( "((?<=/)|((?<=^)(?=\\w)))(?!(\\w|\\\\\\.)+/.*).*", "" ) ); 

Here's the test:

 public static void main( String[] args ) throws IOException { String[] paths = {"/abc/def*/bc", "/abc/def*", "/ab.c/def*", "/ab\\.c/def*", "abc*", "abc/bc"}; for ( String path : paths ) { System.out.println( path + " --> " + path.replaceAll( "((?<=/)|((?<=^)(?=\\w)))(?!(\\w|\\\\\\.)+/.*).*", "" ) ); } } 

Conclusion:

 /abc/def*/bc --> /abc/ /abc/def* --> /abc/ /ab.c/def* --> / /ab\.c/def* --> /ab\.c/ abc* --> abc/bc --> abc/ 

You can configure it from there.

+3
source

Use this regex pattern

 ^/([a-zA-Z]+[a-bA-Z0-9]+)/?.$ 

This will give you the root path with the additional condition that the first letter must be a letter

If it could also be a number, you can just use -

 ^/([a-bA-Z0-9]+)/?.$ 
0
source

All Articles