A
Alan
I do not understand why the regular expression string in the code
below is giving me lines of text and not paragraphs. I am trying to
get the start and end of the whole, repeated pattern.
The output I am getting is:
Chunk: one
What I was expecting was:
Chunk: one
two
three
Can anyone explain this to me? Thank you, Alan
import java.util.regex.*;
public class TextProcessor
{
public static void main(String[] args)
{
String TestString = "one \n two \n three \n\n Another Paragraph";
System.out.println ( "Chunk: " + getChunk ( TestString, 0 ) );
System.out.println ("\n");
}
private static final Pattern PARA_PATTERN = Pattern.compile("(^.*\\S+.*
$)+", Pattern.MULTILINE);
public static String getChunk ( String InputString, int
StartPosition )
{
String OutputString = "";
Matcher matcher = PARA_PATTERN.matcher ( InputString );
try
{
if ( matcher.find ( StartPosition ) )
{
OutputString = InputString.substring(matcher.start(),
matcher.end());
}
}
catch ( IndexOutOfBoundsException e ) { e.printStackTrace();}
catch ( IllegalStateException e ) { e.printStackTrace();}
return OutputString;
}
}
below is giving me lines of text and not paragraphs. I am trying to
get the start and end of the whole, repeated pattern.
The output I am getting is:
Chunk: one
What I was expecting was:
Chunk: one
two
three
Can anyone explain this to me? Thank you, Alan
import java.util.regex.*;
public class TextProcessor
{
public static void main(String[] args)
{
String TestString = "one \n two \n three \n\n Another Paragraph";
System.out.println ( "Chunk: " + getChunk ( TestString, 0 ) );
System.out.println ("\n");
}
private static final Pattern PARA_PATTERN = Pattern.compile("(^.*\\S+.*
$)+", Pattern.MULTILINE);
public static String getChunk ( String InputString, int
StartPosition )
{
String OutputString = "";
Matcher matcher = PARA_PATTERN.matcher ( InputString );
try
{
if ( matcher.find ( StartPosition ) )
{
OutputString = InputString.substring(matcher.start(),
matcher.end());
}
}
catch ( IndexOutOfBoundsException e ) { e.printStackTrace();}
catch ( IllegalStateException e ) { e.printStackTrace();}
return OutputString;
}
}