You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-user@jakarta.apache.org by "Daniel F. Savarese" <df...@savarese.org> on 2002/08/18 06:04:53 UTC

Re: can't substitute in a newline?

In message <5....@mail.telerama.com>, Phillip Rhod
es writes:
>I am trying to do a substitution where I will append a new line to a 
>match.  I am trying to make a lot of code more readable by putting new 
>lines in.
>
>So given a regex of (if.*)
>and a substitution of $1\r\n
>
>Using ORO, it matches, but the newline does not appear.  I tried escaping 

import org.apache.oro.text.regex.*;

public final class foo {
  public static final void main(String args[])
    throws MalformedPatternException
  {
    PatternMatcher matcher = new Perl5Matcher();
    Pattern pattern = null;
    PatternCompiler compiler = new Perl5Compiler();
    String regularExpression, input, result;
    Substitution sub;

    regularExpression = "(if.*)";
    input             = "if foobar\nif baz";
    sub = new Perl5Substitution("$1\r\n", Perl5Substitution.INTERPOLATE_ALL);

    pattern = compiler.compile(regularExpression);

    result = 
      Util.substitute(matcher, pattern, sub, input, Util.SUBSTITUTE_ALL);
    System.out.print("input:\n" + input);
    System.out.print("\noutput:\n" + result);
    System.out.flush();


    input = "if";

    result = 
      Util.substitute(matcher, pattern, sub, input, Util.SUBSTITUTE_ALL);

    if("if\r\n".equals(result))
      System.out.println("Carriage return and line feed are present.");
    else
      System.out.println("Carriage return and line feed NOT present.");
  }
}


/tmp/foo> java foo
input:
if foobar
if baz
output:
if foobar

if baz
Carriage return and line feed are present.



I just want to make a simple request that folks include a short code
snippet that unambiguously represents and reproduces the problem being
encountered.  It saves time in understanding the problem and makes it more
likely that someone will take the time to try to help out.  For example, I
had to take the time to write the simple test to demonstrate the problem
as described does not exist.  But I still don't understand the description
of the problem, so my test may not have any bearing on it.  Unambiguous
code examples reproducing a problem guarantee faster problem resolutions.

daniel



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>