2

Here is my example file :

lineone one
RUN lineone two
lineone three
RUN lineone four

I want to select all lines not starting with run, here is how I did it :

^([^RUN])

Is it possible to match all lines not starting with RUN and then append them to the back of the previous line ? like this

lineone one RUN lineone two
lineone three RUN lineone four
1
  • 1
    Your question doesn't describe what your example shows. In your example you are appending lines that do start with RUN to the end of the previous line. Commented Dec 29, 2010 at 13:12

3 Answers 3

6

If your example is correct you just need to replace "\nRUN" with " RUN".

System.out.println(yourString.replaceAll("\nRUN", " RUN"));

Result:

lineone one RUN lineone two
lineone three RUN lineone four

ideone

Sign up to request clarification or add additional context in comments.

Comments

3

use str.startsWith("RUN");

Comments

0

First of all

^([^RUN]) 

does not work correctly as it will match any line that does not start with either R, U or N.

You should use lookahead:

^(?!RUN)

This should do what you want:

Pattern p = Pattern.compile("\n(RUN)", Pattern.DOTALL);
Matcher matcher = p.matcher("lineone one\nRUN lineone two\nlineone three\nRUN lineone four");
String replaceAll = matcher.replaceAll(" $1");
System.out.println(replaceAll);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.