1

Problem

In my Java application I have the code

Paths.get(v_DirBufferdata).resolve(Paths.get(v_DirIndata).relativize(Paths.get(c_FileIndataLocation))).toString();

with

v_DirBufferdata being "file://workflow/../buffer/"

v_DirIndata being "/tmp/C4243/indata"

c_FileIndataLocation being "/tmp/C4243/indata/one_doc/pv.pdf"

and I would like to get

"file://workflow/../buffer/one_doc/pv.pdf".

however I am getting

"file:/workflow/../buffer/one_doc/pv.pdf".

Regex-Solution?

One simple idea to get the protocol-slash back is with regular expressions. However I want to make sure that only the protocol error is dealt with - not a folder called "file:" by any chance.

Specifically "file:/" should be replaced with "file://", but if "file://" is already there, I do not want to get "file:///". Also, "file:/" should only be replaced if it is at the beginning of the string. So far I've got:

"file:/".replaceFirst("^file:/[^/]", "file://")

Obviously this does not work yet. How do I do that?

Alternative solution

If there is a different solution, which does not require regex-hacking, I am all open for it too.

1

1 Answer 1

1

Either use negative lookahead:

s.replaceFirst("^file:/(?!/)", "file://")

or just capture the one character that comes after :/ and use $1 in the replacement:

s.replaceFirst("^file:/([^/])", "file://$1")
Sign up to request clarification or add additional context in comments.

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.