Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an efficient way to parse a String in Java?

How should I parse the following String using Java to extract the file path?

? stands for any number of random charaters

_ stands for any number of white spaces (no new line)

?[LoadFile]_file_=_"foo/bar/baz.xml"?

Example:

10:52:21.212 [LoadFile] file = "foo/bar/baz.xml"

should extract foo/bar/baz.xml

like image 852
Philipp Avatar asked Jul 29 '09 14:07

Philipp


1 Answers

String regex = ".*\\[LoadFile\\]\\s+file\\s+=\\s+\"([^\"].+)\".*";

Matcher m = Pattern.compile(regex).matcher(inputString);
if (!m.find()) 
    System.out.println("No match found.");
else
    String result = m.group(1);

The String in result should be your file path. (assuming I didn't make any mistakes)

You should take a look at the Pattern class for some regular expression help. They can be a very powerful string manipulation tool.

like image 104
jjnguy Avatar answered Oct 10 '22 12:10

jjnguy