Sitemap

Amicuk Programming Answers

How can I return all line in a file that matches using regexp

2015-10-11   Views:1

Advertisement

Hi! I want to return whole line from a file. This lines must to contains matches for a pattern. How Can I do this. I make some samples, but only return the matches segment of the line. Sample code to look for 00:00 If I put .*00:00.* in the pattern t

Hi!
I want to return whole line from a file. This lines must to contains matches for a pattern.
How Can I do this.
I make some samples, but only return the matches segment of the line.
Sample code to look for 00:00
If I put .*00:00.* in the pattern this return whole line, but is to slow in this file about 300,000 lines.
If I put just 00:00 in the pattern only return the 00:00, but so fast.
What can I Do, or I must use indexOf in a sequential read?
          try {
               // Create matcher on file
               Pattern pattern = Pattern.compile(".*00:00.*");
               Matcher matcher = pattern.matcher(fromFile("logusers_0712"));
               // Find all matches
               while (matcher.find()) {
                    // Get the matching string
                    String match = matcher.group();
                                 //Here I display the matches
                    System.out.println(match);
          } catch (IOException e) {
               e.printStackTrace();
          }Thanks in advance

The replay answer
Advertisement
My two cents:
Grep is a Bad Idea (TM). It is not java and itties
es the solution to a the operational system.Not necessarily bad. Like you say--we don't know much
about the OP's requirements, so grep may be just
fine.
!java != badYour assertion is true. But creating an solution tied to the system when you can do it pure java does not seem like a good Idea especially when we not even know which system he is using. What if he is using Windows or MacOS? Sure he can use grep on non *nix systems, but that would fore him to install the necessary tools, making his solution even more complex.
>
About using indexOf, it is a little limited, not
ot knowing what exactly the OP is searching for itis
difficult to say if it is enough. RegEx would workin
both ways.He said he was searching for "00:00". I think that was just a sample. He apears to be scanning a log file as far as I understood. 00:00 could be a timestamp for whose he wants to see the entries. Using a RegEx make the job easier, like "01:\\d{2} am" to get the whole range of entries inside that morning hour.
If it's really
that literal string he needs to match, as opposed to
say "\\d\\d:\\d\\d" then indexOf is simpler. It will
probably perform better too. I don't know if that
difference will be significant, but on a file of
300,000 lines, it might be noticable.The only way to know that is testing both solutions. But you are right, we are just wondering here, with no better info on the requirements anything can go. I agree with you, indexOf is the simple way to go and probably will sufice.
May the code be with you.

Go to See the other 15 answers

How can I return all line in a file that matches using regexp

Category:DefaultRelease time:2015-10-11Views:130

Hi! I want to return whole line from a file. This lines must to contains matches for a pattern. How Can I do this. I make some samples, but only return the matches segment of the line. Sample code to look for 00:00 If I put .*00:00.* in the pattern t[More]

Problem with "carriage Return" or "Line Feed" in a table

Category:DefaultRelease time:2015-10-11Views:130

Hello, I need help with the function Zeichen(), so called it in german, I'm not sure if it is char() in english. In Pages Version 4.0.1 (746) I've created a table in Pages with this function to make an "carriage Return" in a cell. Here an exampl[More]

How to find a carriage return and Line feed?

Category:DefaultRelease time:2015-10-11Views:130

I need to detect if a crriage retunr and line feed present on each row in my text file before allowing user to upload the text file I know how to remove them from each line but what I need is to detect them no removing them from file. Our concern is[More]

Unable to find line break between two lines in attachment file.

Category:DefaultRelease time:2015-10-11Views:130

Dear all I will be very great full if someone help me out, I am trying to send mail through SMTP server with an attachment of oracle report, but I am unable to find line break between two lines, when I down load the attachment from mail and open atta[More]

Appending to new line in text file

Category:DefaultRelease time:2015-10-11Views:130

I am trying to append text to a new line to a file. I set the append parameter to true and I try to output the new line character after I write the string to the file. However it is still appending the next string to the end of the first rather than[More]

How do i add a newline or carriage return to make my xml file look pretty?

Category:DefaultRelease time:2015-10-11Views:130

Hi guys I'm writing an xml file with jsp, the problem is, it writes it in one big chunk of text. e.g. <start>hello, my name is joe</start><middle>I like trees</middle><end>boo</end> Now what I want to do is present this[More]

How can I input read a line from a file and output it into the screen?

Category:DefaultRelease time:2015-10-11Views:130

How can I input read a line from a file and output it into the screen? If I have a file contains html code and I only want the URL, for example, www24.brinkster.com how can I read that into the buffer and write the output into the screen that using J[More]

How to get the number of lines of a file?

Category:DefaultRelease time:2015-10-11Views:130

Folks: Is there a way to get the number of lines of a text file? I don't know if there is an existing method to do that. Thanks a lot.HI I found some solution other than increment loop and all here is the code it might helpful to u //returns the numb[More]

Hot
I am updating a site that has a flash gallery displayed within a table cell.  I want to replace the flash (.swf) gallery with an html one that would be apple friendly.  Is there a way to embed an html/java slideshow within a table on a page? Lightroo [More]
Hi... I am not sure what generation I have After updating, I does not start at all, no light signal. Just picture show to me to use mini USB for Song logo. How can I do?Welcome to the Apple Community. If your problem persists get yourself a micro USB [More]
Hi, i have a doubt, can i have my pdf files on my sd, edit them while in class with no internet connection and when back online they will sync, all i want is to able to open my files without wifi and edit them but when back online sync their changes [More]
Hai               i want to create a plugin for swf,and added into the Strobe media Playback.i follow this link:http://osmf.org/dev/osmf/OtherPDFs/osmf_plugin_dev_guide.pdf But my plugin is Not Loaded ,i dunno wats the reason.Anybody kindly Help me,a [More]
Running OS X 10.10.1, on each login I'm prompted twice for an iCloud password. How can I turn this off? iCloud is not listed at system preferences / users and groups / login items, which is where I expect to see apps selected as startup apps. See the [More]
This is frustrating. If someone sends me a calendar event that lasts multiple days from Calendar software that ACTUALLY WORKS  (hint hint hint Google Calendar, Outlook), and if I accept this event, it only displays the FIRST DAY of the event in iCal' [More]
Dear Sirs I hope you're doing fine. While activating InfoCube 0PUR_C01 I get the following error: <i><b>RSAU709 : Chararacteristic 0CALQUARTER is available in version M but not in version D.</b></i> (Yes, the word <i>characte [More]
Hi, I am trying to call a web service from VC. I am using an HTTPS link as the WSDL. When I access the web service directly via wsnavigator it is working, when I use VC I am getting: Portal request failed. Could not execute web service consult admini [More]
since i updated my iphone to ios7, i can no longer download applications and also cannot update my apps. what happened? there is an alert popping and it tells that i should switch to Philippine store.Hi, sallymaesmag.  Thank you for visiting Apple Su [More]
I have read all the posts regarding Epson and snow leopard and I am at a loss and pulling my hair out, cursing apple and jobs, and throwing darts at the snow leopard logo now on my dart board. I am soooooooooooooooo frustrated that I even paid for th [More]