• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Compiling blank lines patterns using regex

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regex to match blank line is

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But doesn't meet client's requirements of matching 2 blank lines, useDelimiter, hasNext()
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is anything being added to logData list?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:is anything being added to logData list?


Now it is. Well spotted.

Winston
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic