Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13314 > unrolled thread

Parse a text file and match more than one line

Started bymike <mikaelpetterson@hotmail.com>
First post2012-04-02 04:36 -0700
Last post2012-04-02 20:08 -0400
Articles 7 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Parse a text file and match more than one line mike <mikaelpetterson@hotmail.com> - 2012-04-02 04:36 -0700
    Re: Parse a text file and match more than one line Knute Johnson <nospam@knutejohnson.com> - 2012-04-02 14:43 -0700
    Re: Parse a text file and match more than one line Martin Gregorie <martin@address-in-sig.invalid> - 2012-04-02 22:09 +0000
    Re: Parse a text file and match more than one line Jim Janney <jjanney@shell.xmission.com> - 2012-04-02 16:50 -0600
      Re: Parse a text file and match more than one line Jim Janney <jjanney@shell.xmission.com> - 2012-04-02 20:57 -0600
    Re: Parse a text file and match more than one line Roedy Green <see_website@mindprod.com.invalid> - 2012-04-02 15:53 -0700
    Re: Parse a text file and match more than one line Arne Vajhøj <arne@vajhoej.dk> - 2012-04-02 20:08 -0400

#13314 — Parse a text file and match more than one line

Frommike <mikaelpetterson@hotmail.com>
Date2012-04-02 04:36 -0700
SubjectParse a text file and match more than one line
Message-ID<3e114ac2-7034-4167-8d67-ac869f6643f1@h20g2000yqd.googlegroups.com>
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




[toc] | [next] | [standalone]


#13320

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-04-02 14:43 -0700
Message-ID<jld6hl$h5q$1@dont-email.me>
In reply to#13314
On 4/2/2012 4:36 AM, mike wrote:
> 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>

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#13321

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2012-04-02 22:09 +0000
Message-ID<jld837$v14$1@localhost.localdomain>
In reply to#13314
On Mon, 02 Apr 2012 04:36:56 -0700, mike wrote:

> 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.
   

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#13322

FromJim Janney <jjanney@shell.xmission.com>
Date2012-04-02 16:50 -0600
Message-ID<2pvclh3g0e.fsf@shell.xmission.com>
In reply to#13314
mike <mikaelpetterson@hotmail.com> writes:

> 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.

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#13341

FromJim Janney <jjanney@shell.xmission.com>
Date2012-04-02 20:57 -0600
Message-ID<2pr4w534k6.fsf@shell.xmission.com>
In reply to#13322
Jim Janney <jjanney@shell.xmission.com> writes:

> mike <mikaelpetterson@hotmail.com> writes:
>
>> 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.

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#13324

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-02 15:53 -0700
Message-ID<u8bkn7dkdl7re90u49d07h7jrmb3j02vie@4ax.com>
In reply to#13314
On Mon, 2 Apr 2012 04:36:56 -0700 (PDT), mike
<mikaelpetterson@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>
>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..

[toc] | [prev] | [next] | [standalone]


#13333

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-04-02 20:08 -0400
Message-ID<4f7a3f85$0$293$14726298@news.sunsite.dk>
In reply to#13314
On 4/2/2012 7:36 AM, mike wrote:
> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web