I am in the middle of making a Java application which in particular takes a relative file path of the form

```
String path = "path/to/Plansystem/Xslt/omraade/../../../Kms/Xslt/Rense/Template.xslt"

```

and reduce / simplify the path expression, so that it provides an equivalent path, but without the double dots. That is, we should obtain this String:

```
String result = "path/to/Kms/Xslt/Rense/Template.xslt"

```

Currently, I have defined the following Regular expression: 

```
String parentDirectory = $/\/(?!\.)([\w,_-]*)\.?([\w,_-]*)\/\.\.\//$

```

I then replace any match with a single slash. This approach seems to work, and I came up with the expression using [Regexr.com](https://regexr.com), but it seems to me that my approach is a little hacky, and I would be surprised if this specific functionality is not available in some well tested, well developed library. Is anyone familiar with such a library?

Edit:
Based on the responses made by rzwitserloot and Andy Turner I realized that the following methods works for me:

```
public static String slash = "/"
public static final String backslashes = $/\\+/$

static String normalizePath(String first, String... more) {
      String pathToReturn = Paths.get(first, more).normalize().toString().replaceAll(backslashes, slash)
      return pathToReturn
}

```

Note that the replacement I make at the end is only due to a specific need I have, where I want to preserve the unix notation (even when running on Windows).