Parse a text file and match more than one line

M

mike

Hi,

I am trying to figure out how to use regexp in java to match this
pattern:


compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on
next line . If there is then I need to check if there is an empty
line. If all conditions are fullfilled then I can I know that my build
step is completed and I have a full match.

Any ideas?

br,

//mike
 
K

Knute Johnson

Hi,

I am trying to figure out how to use regexp in java to match this
pattern:


compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on
next line . If there is then I need to check if there is an empty
line. If all conditions are fullfilled then I can I know that my build
step is completed and I have a full match.

Any ideas?

br,

//mike

import java.util.regex.*;

public class test {
static String str = "Hi,\nI am trying to figure out how to use
regexp in java to match this\npattern:\n\ncompile:\n [javac]
Compiling 933 source files to
/tmp/gdduser/classes\n\ndft.properties:\n\nSo I want to make sure I
have:\n\ncompile:\n [javac] Compiling 933 source files to
/tmp/gdduser/classes\n<<nothing on this line>>\n\nHow can I use java to
apply it? It will be something like, when\n\n\"compile:\" is found check
that there is a [javac] Compiling .... on\nnext line . If there is then
I need to check if there is an empty\nline. If all conditions are
fullfilled then I can I know that my build\nstep is completed and I have
a full match.\n\nAny ideas?\n\nbr,\n\n//mike\n";

public static void main(String[] args) {
Pattern p = Pattern.compile(
"(compile:\n\\s+\\[javac\\] Compiling \\d+ source files to .*)");
Matcher m = p.matcher(str);

while (m.find())
System.out.println(m.group(1));
}
}

C:\Documents and Settings\Knute Johnson>java test
compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

C:\Documents and Settings\Knute Johnson>
 
M

Martin Gregorie

Hi,

I am trying to figure out how to use regexp in java to match this
pattern:


compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on next
line . If there is then I need to check if there is an empty line. If
all conditions are fullfilled then I can I know that my build step is
completed and I have a full match.

Any ideas?
Write some code, test it against a saved example of the logfile you want
to scan, and if you can't get it to do the job, post an SSCCE here. In
fact, writing an SSCCE as your first attempt would be a good idea. Look
here to find out about writing one: http://pscode.org/sscce.html

I'd probably start by testing regexes with "grep -P" and then make them
work as Java code. If dealing with multi-line regex matching got messy
due to the need to work inside a sliding three line window, I might try
generating a lexical parser with the Coco/R package, though it is
somewhat of a sledge-hammer for this particular nut: its a tool I'm happy
to use because I'm familiar with BNF grammar notation though it could be
quite a learning curve if you're not.
 
J

Jim Janney

mike said:
Hi,

I am trying to figure out how to use regexp in java to match this
pattern:


compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on
next line . If there is then I need to check if there is an empty
line. If all conditions are fullfilled then I can I know that my build
step is completed and I have a full match.

Any ideas?

br,

//mike

If the file is not too large, you can read it all into a single string
and apply a regexp to that. Otherwise, you've already sketched out a
workable approach.

In the general case, consider using a finite state machine.
 
R

Roedy Green

Any ideas?

\\n and \\r will match those control chars
Pattern.compile( x, Pattern.MULTILINE) will cause matches to span
lines.
Read up on ^ and $


--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 
A

Arne Vajhøj

I am trying to figure out how to use regexp in java to match this
pattern:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on
next line . If there is then I need to check if there is an empty
line. If all conditions are fullfilled then I can I know that my build
step is completed and I have a full match.

I fear that you are depending a lot on very specific ant output
format.

Maybe it would be better to build using the compiler API
where you have full control over what is being compiled
and what errors occur.

Arne
 
J

Jim Janney

Jim Janney said:
mike said:
Hi,

I am trying to figure out how to use regexp in java to match this
pattern:


compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes

dft.properties:

So I want to make sure I have:

compile:
[javac] Compiling 933 source files to /tmp/gdduser/classes
<<nothing on this line>>

How can I use java to apply it? It will be something like, when
"compile:" is found check that there is a [javac] Compiling .... on
next line . If there is then I need to check if there is an empty
line. If all conditions are fullfilled then I can I know that my build
step is completed and I have a full match.

Any ideas?

br,

//mike

If the file is not too large, you can read it all into a single string
and apply a regexp to that.

Or...

A matcher needs a CharSequence, not a string, and you can build a
CharSequence from a ByteBuffer, and you can map a ByteBuffer directly to
a file. Put it all together and you get something like this:

http://www.java2s.com/Code/Java/File-Input-Output/ApplyingRegularExpressionsontheContentsofaFile.htm


I'd guess this could be horribly expensive for some kinds of patterns.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top