• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling blank lines patterns using regex

 
Ashish Mishra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
regex to match blank line is

 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But doesn't meet client's requirements of matching 2 blank lines, useDelimiter, hasNext()
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is anything being added to logData list?
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:is anything being added to logData list?

Now it is. Well spotted.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic