Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to strip all directorynames from Path (leave filename)

Tags:

java

string

regex

I want to remove all directorynames from a path:

Payload/brownie.app/Info.plist

should become

Info.plist

What regex should I use or can I use replace() from String in java? thanks!

like image 491
tzippy Avatar asked Jan 29 '11 18:01

tzippy


People also ask

What does \s+ mean in regex?

On the other hand, the \S+ (uppercase S ) matches anything that is NOT matched by \s , i.e., non-whitespace. In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example, \w for word character and \W for non-word character; \d for digit and \D or non-digit.

How do you escape the colon in regex?

By placing - at the start or the end of the class, it matches the literal "-" . As mentioned in the comments by Keoki Zee, you can also escape the - inside the class, but most people simply add it at the end. You can also escape the hyphen with a backslash, [a\-z] .

How to get directory names from a path?

Use os. path. dirname() to get the directory folder (name) from a path string.

How to get file path from filename in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.


1 Answers

Try with this:

new File("Payload/brownie.app/Info.plist").getName()

This returns the filename without directories.

Example:

String filename = new File("Payload/brownie.app/Info.plist").getName();
System.out.println(filename);

Outupt:

Info.plist
like image 95
Arnaud Le Blanc Avatar answered Oct 17 '22 09:10

Arnaud Le Blanc