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 / First Aid / June 2006

Tip: Looking for answers? Try searching our database.

help using pattern and matcher classes

Thread view: 
Petterson Mikael - 19 Jun 2006 15:20 GMT
Hi,

I have two words that I need to search my files for.

DONUMBER and DO_NUMBER.

I am not sure how I can create two patterns to search for in the same
file. Also what is the difference between the pattern and matcher (since
you can use regex in both).

This is the code I will use:

try {
           // Create the reader
           String filename = "infile.txt";
           String patternStr1 = "DONUMBER";
           String patternStr2 = "DO_NUMBER";
           BufferedReader rd = new BufferedReader(new FileReader(filename));
   
           // Create the pattern
           Pattern pattern = Pattern.compile();
           Matcher matcher = pattern.matcher("");
   
           // Retrieve all lines that match pattern
           String line = null;
           while ((line = rd.readLine()) != null) {
               matcher.reset(line);
               if (matcher.find()) {
                   // line matches the pattern
               }
           }
       } catch (IOException e) {
       }

All hints are very much welcome.

cheers,

//mikael
IchBin - 19 Jun 2006 20:14 GMT
> Hi,
>
[quoted text clipped - 17 lines]
>    
>             // Create the pattern

            Pattern pattern1 = Pattern.compile(patternStr1);
            Pattern pattern2 = Pattern.compile(patternStr2);
            Matcher matcher1 = pattern1.matcher("");
            Matcher matcher2 = pattern2.matcher("");

            // Retrieve all lines that match pattern
            String line = null;
            while ((line = rd.readLine()) != null)
            {
                matcher2.reset(line);
                matcher1.reset(line);
                if (matcher2.find() || matcher1.find())
>                     // line matches the pattern
>                 }
[quoted text clipped - 7 lines]
>
> //mikael

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
IchBin - 19 Jun 2006 20:22 GMT
>> Hi,
>>
[quoted text clipped - 34 lines]
>>         } catch (IOException e) {
>>         }

Also take a look at this "Implementing a FilterReader to Filter Lines
Based on a Regular Expression" in the The Java Developers Almanac

http://javaalmanac.com/egs/java.util.regex/LineFilter2.html

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Dale King - 28 Jun 2006 02:36 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> I am not sure how I can create two patterns to search for in the same
> file.

In this case you should be able to use a single pattern. The trivial way
to search for two possibilities is to just use or as in
"DONUMBER|DO_NUMBER".

But in this case you can instead use "DO_?NUMBER".

> Also what is the difference between the pattern and matcher (since
> you can use regex in both).

Pattern just specifies a regular expression to search for. It is a
compiled form of the regular expression.

Matcher is the one that actually searches a particular sequence of
characters for the pattern and keeps track of where it was found, etc.

You don't really use a regex in both. You use a regex to create a
Pattern and you use a Pattern to create a Matcher.
Signature

 Dale King



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.