• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"parsing file"

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 31
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic