Solved

Java / Linux and Regular Expressions

Posted on 2016-11-10
11
43 Views
Last Modified: 2016-11-11
From within Java, I want to search an external text file called log.txt, starting at the bottom of the file, and read backwards, reading a word at a time, until I find a string sequence that starts with any a space, then any 5 Capital Letters, followed by a underscore, followed by the word "Forecast"

So, starting from the bottom of the file and working up, find the first occurrence of something like this:  " RAWV2_Forecast"

Ideally I would get the 5 letter sequence returned to the Java program
0
Comment
Question by:MarkLoveExEx
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
Hi!

Look at these examples how to code regular expressions in Java.
http://www.java2s.com/Code/Java/Regular-Expressions/CatalogRegular-Expressions.htm

This example here could be a starting point to follow and code your problem
http://www.java2s.com/Code/Java/Regular-Expressions/Printallthestringsthatmatchagivenpatternfromafile.htm
Where you have the pattern something like this
Pattern patt = Pattern.compile("[A-Za-z]RAWV2_Forecast[A-Za-z]+");

Open in new window


Regards,
    Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
How big is the file?

... then any 5 Capital Letters
In point of fact, one of the characters in your example is a number - which is it?
1
 
LVL 24

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 250 total points
Comment Utility
Hi!

This pattern should give you all the  5 Capital Letters, followed by a underscore, followed by the word "Forecast"  

Pattern patt = Pattern.compile("[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]_Forecast[A-Za-z0-9]+");

Open in new window


Regards,
    Tomas Helgi
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
Comment Utility
Read it all into one String, unless the file is too big to do so. The pattern will then be (to match the example rather than the description)

"\\s+[A-Z0-9]{5}_Forecast.*?$"

Open in new window


That will encourage the engine to search from the end backwards
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
Hi

Processing a file line by line is faster than loading the whole file into memory (a string)
especially with large files. Then this  approach as I mentioned in earlier comment
is better.

Regards,
    Tomas Helgi
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Processing a file line by line is faster than loading the whole file into memory
That's not normally the case and leads me to think that you probably don't know how file lines are processed in Java - actually not so trivial a task
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
HI!

Working with files that are 10+ or even 100+ GB in size will lead to OutOfMemoryException where
the approach is to load the whole file into a String.

http://www.baeldung.com/java-read-lines-large-file

Regards,
     Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Well obviously if the file is huge - yes. That's why i asked how large it was
0
 

Author Comment

by:MarkLoveExEx
Comment Utility
Thank you all for your input.  CEHJ, the file is not very big.  It is actually a log file that will grow over time, but it gets deleted every day.  I am not on the system at the moment, so I can't check its size right now.
0
 

Author Closing Comment

by:MarkLoveExEx
Comment Utility
Thank you.  CEHJ...you're right, I do have a number in there with those letters.  I must be tired.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Well obviously if the file is huge - yes

. . .  and of course if it ever is huge, you can use nio's FileChannel and MappedByteBuffer classes to chop it up and process it in very large chunks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now