How to select file path using regular expression

I would like to create a java regex that selects everything from a file: up to the last forward slash (/) in the file path. This means that I can replace it in another way.

<!DOCTYPE "file:C:/Documentum/XML%20Applications/joesdev/goodnews/book.dtd"/>
<myBook>cool book</myBook>

      

Does anyone have any ideas? Thank!!

0


source to share


4 answers


You just want to go to the last forward slash before the end quote, don't you? If yes:

file:[^"]+/



(the line "file:", then nothing but ", ending with /)
Correctly escaped:

String regex = "file:[^\"]+/";

      

+3


source


Try the following:



"file:.*/[^/]*"/>

0


source


You can try to handle this yourself, but a better scheme would be to just separate the parts between the quotes and use java.util.File to separate the directory name from the file name. This way you don't have to worry about / vs \ or various escape characters.

0


source


String newPath = "C:/Documentum/badnews";
String originalPath = "<!DOCTYPE \"file:C:/Documentum/XML%20Applications/joesdev/goodnews/book.dtd\"/>";
System.out.println(originalPath.replaceFirst("file:C:((/[/\\w%]+))", newPath));

      

0


source







All Articles