Thursday, 15 April 2010

Java Regex Multiline issue -


I have a file that reads through the Apache Commands FileUtils.readFileToString, which has the following format:

& lt pre & lt ;! - Support [manual modification of header can cause parsing problem!] / - & gt; & Lt; -! LOGGINGVERSION [2.0.7.1006] / - & gt; & Lt; -! NAME [./log / defaultTrace_00.trc] / - & gt; & Lt; -! Method [defaultTrace_00.trc] / - & gt; & Lt; -! FORMATTER [com.sap.tc.logging.ListFormatter] / - & gt; & Lt; -! ENCODING [UTF8] / - & gt; & Lt ;! - FILESET [0, 20, 10485760] / - & gt; & Lt; -! PREVIOUSFILE [defaultTrace_00.19.trc] / - & gt; & Lt; -! NEXTFILE [defaultTrace_00.1.trc] / - & gt; & Lt; -! ENGINEVERSION [7.31.3301.368426.20141205114648] / - & gt; & Lt; -! LOGHEADER [end] / - & gt; # 2.0 # 2015 03 04 11: 04: 19: 687 # + 0100 # Debug # ... (few lines to follow)

I am filtering everything between LOGHEADER I'm trying to [START] and LOGHEADER [END] line so I created a Java REGEX:

  string file resource = FileUtils.readFileToString (file); String loghard = "लोहादार \\ [start \\]. * लोहादार \\ [END \\]"; Pattern p = Pattern.compile (logheader, Pattern.DOTALL); Mitcher M = P. MATTURE (file content); Println (m.matches ());  

(Since it is a multinline pattern and I want to cover line breaks too) but this pattern does not match the string. If I try to remove the LOGHEADER \ [END \] portion of Reighx, then I get a match, it contains the whole string. I do not know why it does not match for the original RegEx.

Any help is appreciated - Thanks a lot!

This Java matches () is important to remember about the method That your regular expression should match the whole line.

Then, you can and & lt ;! - LOGHEADER [START] / - & gt; and n & lt ;! - Use Find () to capture among all! -LOGHEADER [END] / - :

  string logger = "(? & Lt; = LOGHEADER \\ [START \\] / - & gt;). * (? = & Lt ;! - LOGHEADER \\ [end \\]) "; Pattern p = Pattern.compile (logheader, Pattern.DOTALL); Mitcher M = P. MATTURE (file content); While (m.find ()) {System.out.println (m.group ()); }  

Or, to follow the suggestion that you suggest (just matches ), send us the ^. * and . $$ :

  string logheader = "^. * LOGHEADER \\ [START \\]. * LOGHEADER \\ [END \\]. $ $"; Pattern p = Pattern.compile (logheader, Pattern.DOTALL); Mitcher M = P. MATTURE (file content); Println (m.matches ());  

No comments:

Post a Comment