• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"parsing file"

 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic