| Author |
Compiling blank lines patterns using regex
|
Ashish Mishra
Greenhorn
Joined: Jan 03, 2007
Posts: 3
|
|
Hi,
I am new to regex. I am kind of writing a protocol log analyzer, the logs look like following -
abc:
xyz:
blah blah blah !
no no
true = yes
123:
909:
wow wow wow !
no no
true = no
mno:
tyz:
alpha beta gama
no no
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<br />SCJP 1.4
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
regex to match blank line is
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4726
|
|
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.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
But doesn't meet client's requirements of matching 2 blank lines, useDelimiter, hasNext()
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4726
|
|
Harsha Smith wrote:But doesn't meet client's requirements of matching 2 blank lines
No, but:does (at least as I understand it).
useDelimiter, hasNext()
@Ashish: I disagree with this advice, for the reasons I stated above; but you must make your own decision.
Winston
Edit: well spotted.
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
|
is anything being added to logData list?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4726
|
|
Harsha Smith wrote:is anything being added to logData list?
Now it is. Well spotted.
Winston
|
 |
 |
|
|
subject: Compiling blank lines patterns using regex
|
|
|