1

Say I have a java source file saved into a String variable like so.

String contents = Utils.getTextFromFile(new File(fileName));

And say there is a line of text in the source file like so

String x = "Hello World\n";

Notice the newline character at the end.

In my code, I know of the existence of Hello World, but not Hello World\n so therefore a call to

String search = "Hello World";
contents = contents.replaceAll(search, "Something Else");

will fail because of that newline character. How can I make it so it will match in the case of one or many newline characters? Could this be a regular expression I add to the end search variable?

EDIT:

I am replacing string literals with variables. I know the literals, but I dont know if they have a newline character or not. Here is an example of the code before my replacement. For the replacment, I know that application running at a time. exists, but not application running at a time.\n\n

int option = JOptionPane.showConfirmDialog(null,"There is another application running. There can only be one application\n" +
"application running at a time.\n\n" + 
"Press OK to close the other application\n" + 
"Press Cancel to close this application",
"Multiple Instances of weh detected", 
JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);

And here is an example after my replacement

int option = JOptionPane.showConfirmDialog(null,"There is another application running. There can only be one application\n" +
"application running at a time.\n\n" + 
"Press OK to close the other application\n" + 
"PRESS_CANCEL_TO_CLOSE",
"MULTIPLE_INSTANCES_OF",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.ERROR_MESSAGE);

Notice that all of the literals without newlines get replaced, such as "Multiple Instances of weh Detected" is now "MULTIPLE_INSTANCES_OF" but all of the ones with new lines do not. I am thinking that there is some regular expression I can add on to handle one or many newline characters when it tries to the replace all.

7
  • It shouldn't matter that the \n exists. Do you want the \n removed as well? Commented Apr 27, 2011 at 17:14
  • For some reason, all the Hello Worlds without the newline get replaced, but all the ones with it do not. Commented Apr 27, 2011 at 17:14
  • contents = contents.replaceAll("Hello World", "Something Else"); should make your example text "Something Else\n". Would you like it to be just "Something Else"? Commented Apr 27, 2011 at 17:15
  • 1
    Just saw your above comment. Could you post some of your real data so we can take a look? Commented Apr 27, 2011 at 17:16
  • It doesn't matter to me if the newline character is there or not after the replacement, i just need it to actually to the replacement. Commented Apr 27, 2011 at 17:17

5 Answers 5

1

well since its actually a regular expression that is passed as the first argument you could try something like this as the first argument

String search = "[^]?[H|h]ello [W|w]orld[\n|$]?"

which will search for hello world everywhere in the start and the end of a line wether it has a \n or not.

a bit redundant and as stated it should not matter but... apparantly it does

try it (made it nifty so it matches capital as well as regular letters :P... just overdoin it)

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

1 Comment

the [\n|$]? might even be better if placed as just [$]? since \n is recognized as the end of a line ($).
1

If you're only replacing string literals, and you're ok with replacing every occurrence of the literal (not just the first one) then you should use the replace method instead of replaceAll.

Your first example should change to this:

String search = "Hello World";
contents = contents.replace(search, "Something Else");

The replaceAll does a regular expression replacement instead of a string literal replacement. This is generally slower, and is not strictly necessary for your use case.

Note that this answer assumes that the trailing newline characters can be left in the string (which you've said you are ok with in the comments).

Comments

1
String search = "Hello World\n\n\n";                           
search.replaceAll ("Hello World(\\n*)", "Guten Morgen\1");

\1 captures the first group, marked by (...), counting from the opening parenthesis. \n+ is \n for newline, but the backslash needs to be masked in Java, leading to two backslashes. * means 0 to n, so it would catch 0, 1, 2, ... newlines.

Comments

0

As the commenters noted, the \n should not mess this up. However, if you are ok with removing the new lines, you can try this:

contents = contents.replaceAll("search"+"\\n*", "Something Else");

Comments

0

As per your question you need to pass following charator

public String replaceAll(String regex, String replacement)

The two parameters are –

regex – the regular expression to match
replacement – the string to be substituted for every match

Some other method is: -

replace(char oldChar, char newChar)
replace(CharSequence target, CharSequence replacement)
replaceFirst(String regex, String replacement)

example is

import  java.lang.String;

public class StringReplaceAllExample {

public static void main(String[] args) {

String str = "Introduction 1231 to  124 basic 1243 programming 34563 concepts 5455";

String Str1 = str.replaceAll("[0-9]+", "");

System.out.println(Str1);

Str1 = str.replaceAll("[a-zA-Z]+", "Java");

System.out.println(Str1);

}

}

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.