1

Is possible to substring this String = "D:/test/for test/change.txt:D:/test/for test/further.txt:D:/test/for test/yandex.txt" to:

D:/test/for test/change.txt

D:/test/for test/further.txt

D:/test/for test/yandex.txt

Because are two column, I can not split() use ":".

2
  • i think the best logic is replace :D: to {SPACE}D: and split by space eg. replace(":D:"," D:").split(" "); Commented Oct 5, 2021 at 19:54
  • @DilermandoLima There is a space in the path itself as well. That's not going to work. Commented Oct 5, 2021 at 20:00

3 Answers 3

2

It looks to me like you want to split on a colon whenever it's not followed by a slash. For that, the regular expression you need is a "negative lookahead", which means "not followed by". So you should write myString.split(":(?!/)") - the (?! ) is how to write the negative lookahead.

Sign up to request clarification or add additional context in comments.

Comments

2

You can simply split it on :(?=[A-Za-z]:\/) which can be explained as

  • : : The character :
  • (?= : Start of lookahead
    • [A-Za-z] : An alphabet
    • :\\/ : The character : followed by /
  • ) : End of lookahead

Demo:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "D:/test/for test/change.txt:D:/test/for test/further.txt:D:/test/for test/yandex.txt";
        String[] paths = str.split(":(?=[A-Za-z]:\\/)");
        Arrays.stream(paths).forEach(System.out::println);
    }
}

Output:

D:/test/for test/change.txt
D:/test/for test/further.txt
D:/test/for test/yandex.txt

ONLINE DEMO

Comments

1

A simple regular expression below splits on ":" that are followed by a "driveletter:"

String s = "D:/test/for test/change.txt:D:/test/for test/further.txt:D:/test/for test/yandex.txt";
s.split(":(?=\\w:)");
==> String[3] { "D:/test/for test/change.txt"
              , "D:/test/for test/further.txt"
              , "D:/test/for test/yandex.txt" }

Note that this won't help if additional paths don't begin with driveletter:

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.