I am new to regex. I am kind of writing a protocol log analyzer, the logs look like following -
blah blah blah !
true = yes
wow wow wow !
true = no
alpha beta gama
true = yes
As you can see each message block is separated by two blank lines.
I need to separate each message and put them in Scanner. Something like -
Scanner lMessageScanner = new Scanner(pInputStream); //input from log file
myMessageScanner.useDelimiter(Pattern.compile("^\\s*$\\n")); //(this pattern is not working for me)
What I want is, when I iterate over myMessageScanner.hasNext()
I can get each message block and further I can grep each values.
Need some help on how to compile this string by using delimiters "two blank lines" and get each message block in my scanner without those lines.
Help appreciated !
Ashish Mishra wrote:Need some help on how to compile this string by using delimiters "two blank lines" and get each message block in my scanner without those lines.
What you're doing seems awfully tortuous to me. Why not just use nextLine() to read your lines?
Then your pattern will be perfect for checking for blank lines (in fact, I prefer yours to Harsha's) except that you don't need the "\\n" (Scanner.nextLine() doesn't return it); and if you get two of them, you know that the next line must be the start of a new log entry.
The other problem with the way you're currently doing it is that unless you do further processing you'll get a multiline string with embedded newlines in it for your log data. While regex patterns can be set up to deal with multiple lines, they generally work best with single line input.
Harsha Smith wrote:But doesn't meet client's requirements of matching 2 blank lines
No, but:does (at least as I understand it).
@Ashish: I disagree with this advice, for the reasons I stated above; but you must make your own decision.
Edit: well spotted.
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Rocket Oven Kickstarter - from the trailbosshttps://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss