Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Parsing multiple lines with regex

Thread view: 
banansol@hushmail.com - 01 Apr 2007 19:37 GMT
Hi,
I'm making a program that seraches a html file for special lines, and
I use the
Scanner class with a regex to find those lines. It is working well,
but now I wan't
to extract data that is on lines directly following my searched line.
If I could make big
regex that included several lines it would be no problem, but since
the method
findInLine() only searches one line at a time, I'm not sure how to do
it.
What is a good way to do this? I don't want a full blown html parser,
just a way to
find some 3-4 lines with same structure that are repeated several
times in a document.

As I said, if I could somehow could get a regex to span multiple
lines, that would be
good, I think. Another idea is to nest the searches, so that when I
find the first line
I could call a method or something that manually gets the remaing
lines and extract
the data from them, but that sounds more complicated. I'm not sure.
Any ideas?

Thanks!
TechBookReport - 02 Apr 2007 11:44 GMT
> Hi,
> I'm making a program that seraches a html file for special lines, and
[quoted text clipped - 22 lines]
>
> Thanks!

Can't you use Pattern.MULTILINE, or am I missing something?

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html

Jeff Higgins - 02 Apr 2007 18:04 GMT
banansol wrote:
> Hi,
>
> As I said, if I could somehow could get a regex to span multiple
> lines, that would be
> good, I think.

I recently had good luck using a regex across several lines using
Scanner.findWithinHorizon(Pattern pattern, int horizon).

If horizon is 0, then the horizon is ignored and this method continues
to search through the input looking for the specified pattern without bound.

> Another idea is to nest the searches, so that when I
> find the first line
> I could call a method or something that manually gets the remaing
> lines and extract
> the data from them, but that sounds more complicated. I'm not sure.
> Any ideas?

while (scanner.hasNext()) {
 for (int i = 0; i < 4; i++) {
   String match = sc.findWithinHorizon(pattern, 0);
     if (match.endsWith(",")) {}
     if (match.startsWith("\"")) {}
     if (match.length() == 0){}
     if(i == 0){}
     else if(i == 1){}
     else if(i == 2){}
     else{}
 }
 list.add(match);
}

> Thanks!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.