wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""parsing file"" Watch ""parsing file"" New topic
Author

"parsing file"

Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Hi,

I have a method in a class that reads a file and returns a String Object.

Another class calls that method and gets the string object. This string contains "patterns" that I have to filter.

Example:
//some content here
Drives
********
cdr LITEON DVD-ROM LTD163 GDHA
cdr HL-DT-ST CD-RW GCE-8400B B104
//some more content here

From this "string" i would like to filter
1. "LITEON DVD-ROM LTD163 GDHA"
2. "HL-DT-ST CD-RW GCE-8400B B104"

I tried to use the Pattern class for regexps.



Is that the best approach to tackle this problem? Or is there some other class in Java that makes this "easier"?

Thanks in advance.
Oscar Costa
Ranch Hand

Joined: Feb 05, 2009
Posts: 31

Hello Viv,

You can use this:


But, this approach is less efficient because it don't reuse the compiled pattern.

See more at: http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

But, this approach is less efficient because it don't reuse the compiled pattern.


And the OP also need the matcher to extract the match (group 1) too...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
I cannot figure out the correct regular expression for this problem.

I have tried the following:

"cdr\\s*([\\w\\p{Punct}]*)\\s*([\\w\\p{Punct}]*)\\s*([\\w\\p{Punct}]*)\\s*([\\w\\p{Punct}]*)\\s*(?:Msg)"

But this gives me only the first line not the info in the second line. (Msg) ... I match everything till this string is matched.
I am new to the regexp "business" and tried to read up some stuff, however still could not figure out the suitable solution.

Thanks for any advice and help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: "parsing file"
 
Similar Threads
Reading index of CD/DVD-ROM
Get the CD-rom drives
Question on DVD drives
Max. Denny's DVD Project: DVDDatabase class doubt
Skip Line/Pattern with Scanner class