Regex to analyze all the way?

Is there a regular expression that can parse a string pointing to a file or folder (from the root, for example C :)?

The language is Javascript.

thank

+3
source share
2 answers

Well, actually there are several, but it depends on what exactly you want to analyze. Do you want something like: "C: /.../.../ nameOfFile.extension"? Or without a file name? Can you clarify the problem? Which input?

I made this code really simple, which for this path gives you a path without root.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestProgram {

    static String path = "C:\\Folder1\\Folder2\\file.extension";

    private static void parsePath() {
      String newPath = "";
      String root = "C:";
      Matcher regexMatcher;
      regexMatcher = Pattern.compile("\\\\").matcher(path);
      newPath = regexMatcher.replaceAll("/");
      regexMatcher = Pattern.compile(root).matcher(newPath);
      newPath = regexMatcher.replaceAll("");    
      System.out.println(newPath);
    }

    public static void main(String[] args) {
       parsePath();    
    }
}

Conclusion:

/Folder1/Folder2/file.extension

I don’t know if you want this, but you just need to play using the methods. You will eventually reach a solution.

+2
source

split?

var path = "C:\\Users\\Joe.Blow\\Documents\\Pictures\\NotPr0n\\testimage1.jpg"
var arrPath = path.split("\\");
var filename = arrPath[arrPath.length - 1];
var drive = arrPath[0];

.

+2

All Articles