I reworked my stratergy ; instead of asking for a bunch of lines when a certain condition is met I parse it a line at a time, looking for certain text like 'type_name: deliver_sm' and getting the required lines, one at a time. It may be a lot of patterns to write but they are all very simillar and easier for me to understand :) . Thanks for your input anyway.

Kumar, Vasanth wrote:

It seems that your expression is correct. Probably you are looking at the result instead of the groups.

I ran TestRegex.java with data.txt as the data you had, and expr.txt as: \s*type_name: deliver_sm\s*([^\f]*)\s*SMPP PDU dump ends.\s*

Below is the result I got. Note the result of Saved Group 1.

---------------------------------------------------------------------------
java TestRegex
Contents: \s*type_name: deliver_sm\s*([^\f]*)\s*SMPP PDU dump ends.\s*
Contents: 2003-03-07 17:20:33 [6] DEBUG:   type_name: deliver_sm
{
   many lines to follow.
}
2003-03-07 17:20:33 [6] DEBUG: SMPP PDU dump ends.


RESULT: Match: type_name: deliver_sm { many lines to follow. } 2003-03-07 17:20:33 [6] DEBUG: SMPP PDU dump ends.

Length: 111
Groups: 2
Begin offset: 30
End offset: 141
Saved Groups:
1: {
   many lines to follow.
}
2003-03-07 17:20:33 [6] DEBUG:
Begin: 26
End: 90

---------------------------------------------------------------------------


-----Original Message----- From: Kwok Peng Tuck [mailto:[EMAIL PROTECTED] Sent: Sunday, March 09, 2003 11:55 PM To: [EMAIL PROTECTED] Subject: Regular expression help.


I'm trying to use a regular expression to obtain the contents of a text file say between the start and end (which can occur many times in the file):


2003-03-07 17:20:33 [6] DEBUG:   type_name: deliver_sm
{
   many lines to follow.
}
2003-03-07 17:20:33 [6] DEBUG: SMPP PDU dump ends.



But the result doesn't seem to work, and will continue on until the end of the file.
Is there something wrong with my pattern ? My pattern looks like this :


String pattern = "\\s*type_name: deliver_sm\\s*([^\\f]*)\\s*SMPP PDU dump ends.\\s*"
I'm using the Perl5Compiler. What can I do to correct the pattern ? I'm kind of new at this.



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



------------------------------------------------------------------------

import java.util.*;
import java.io.*;

import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Util;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.StringSubstitution;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MalformedPatternException;

public class TestRegex
{
 public static void main(String[] args)
 {
   try
   {
     PatternMatcher oroMatcher;
     PatternCompiler oroCompiler;
     Pattern oroPattern;
     PatternMatcherInput oroInput;

String sExpr = readDataFromFile("expr.txt");
String sData = readDataFromFile("data.txt");
oroCompiler = new Perl5Compiler();
oroMatcher = new Perl5Matcher();


// oroPattern = oroCompiler.compile(sExpr, Perl5Compiler.CASE_INSENSITIVE_MASK);

     try
     {
       oroPattern = oroCompiler.compile(sExpr);
     }
     catch(MalformedPatternException e)
     {
       throw new Exception("Bad pattern.", e);
     }

oroInput = new PatternMatcherInput(sData);

while (oroMatcher.contains(oroInput, oroPattern))
{
System.out.println();
System.out.println("RESULT:");
MatchResult result = oroMatcher.getMatch();
// Perform whatever processing on the result you want.
// Here we just print out all its elements to show how its
// methods are used.
System.out.println("Match: " + result.toString());
System.out.println("Length: " + result.length());
int groups = result.groups();
System.out.println("Groups: " + groups);
System.out.println("Begin offset: " + result.beginOffset(0));
System.out.println("End offset: " + result.endOffset(0));
System.out.println("Saved Groups: ");
// Start at 1 because we just printed out group 0
for (int group = 1; group < groups; group++)
{
System.out.println(group + ": " + result.group(group));
System.out.println("Begin: " + result.begin(group));
System.out.println("End: " + result.end(group));
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
StackTraceElement[] stackTraceArray = e.getStackTrace();
System.out.println("Size = " + stackTraceArray.length);
for (int i = 0; i < stackTraceArray.length; i++)
{
StackTraceElement element = stackTraceArray[i];
System.out.println("Class: " + element.getClassName());
System.out.println("File: " + element.getFileName());
System.out.println("Line: " + element.getLineNumber());
System.out.println("Method: " + element.getMethodName());
System.out.println(element.toString());
System.out.println("--------------------------------------------------------------");
}
}
}
static String readDataFromFile(String filename) throws Exception
{
final int bufferSize = 80;
Reader reader;
try
{
reader = new FileReader(filename);
}
catch (FileNotFoundException e)
{
throw new Exception("Error opening filename: " + filename, e);
}


char[] charBuffer = new char[bufferSize];
StringBuffer strBuf = new StringBuffer();
try
{
int amountRead = reader.read(charBuffer, 0, bufferSize);
while (amountRead != -1)
{
strBuf.append(charBuffer, 0, amountRead);
amountRead = reader.read(charBuffer, 0, bufferSize);
}
}
catch (IOException e)
{
throw new Exception("Error while reading file: " + filename, e);
}


   System.out.println("Contents: " + strBuf.toString());
   return strBuf.toString();
 }

}


------------------------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to