Angelina Angelina - 3 months ago 32
Java Question

Regex to strip Drive letter

I am trying to check if there is a

letter
followed by
colon
in my string.

If so, strip both.

F:/users/mark/
=>
/users/mark/


This is what I have so far:

if(Pattern.matches("[a-zA-Z]:", path))
path = path.substring(2);


Does anyone have a better idea on how to accomplish this?

Answer

You can use:

path = path.replaceAll("^[a-zA-Z]:", "");