What is an effective way to parse strings in Java?
•
Java
How should I use java to parse the following string to extract the file path?
? Represents any number of random characters
_ Represents any number of spaces (no new lines)
?[LoadFile]_file_=_"foo/bar/baz.xml"?
Example:
10:52:21.212 [LoadFile] file = "foo/bar/baz.xml"
Foo / bar / Baz should be extracted xml
Solution
String regex = ".*\\[LoadFile\\]\\s+file\\s+=\\s+\"([^\"].+)\".*";
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 the result should be your file path (assuming I didn't make any mistakes)
You should check the pattern class for some regular expression help They can be a very powerful string manipulation tool
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码