Java: how to replace space and \r in string with delimiter value? - java

How to replace space and \r in string with delimiter value?
String oldDelimiter = " ";
String newDelimiter = "o";
String fileContent = "try some **random** %!(chars)!% ##\r" +
"or line break$# \r" +
":(";
fileContent = fileContent.replaceAll("[" + oldDelimiter + "]+", newDelimiter);
fileContent = fileContent.replaceAll("\r", newDelimiter);
current output: tryosomeo**random**o%!(chars)!%o##oorolineobreak$#oo:(
desired output: tryosomeo**random**o%!(chars)!%o##oorolineobreak$#o:(
Notice the extra letter o towards the end of the string after the # symbol.
Update: it should only replace with o if there is a space or \r. However, if both space and \r and next to each other, then only replace with one o delimiter.

Like Andreas said, use the "[ \r]" to replace all of the \r and " " with spaces.
fileContent = fileContent.replaceAll("[ \r]+", "o");

Related

Java regular expression: new line with forward slash in split

I want to capture below from regular expression.
/
--//
or it can be
--//
I tried:
public static final String DELIMITER = "/\\n--//|-//";
ddl.addAll(..).split(DELIMITER)));
and combinations but nothing working.
I am using on Windows
You don't need to escape so much. You are missing a second newline in your delimiter:
String newline = System.getProperty("line.separator");
String text = "before" + "/" + newline + newline + "--//" + "after";
System.out.println(text);
String delimiter = "/" + newline + newline + "--//";
String[] parts = text.split(delimiter);
System.out.println(parts[0]); // prints "before"
System.out.println(parts[1]); // prints "after"

How to replace multiple spaces and newlines with one blank line

How to remove multiple spaces and newlines in a string, but preserve at least one blank line for each group of blank lines.
For example, change:
"This is
a string.
Something."
to
"This is
a string.
Something."
I'm using .trim() to strip whitespace from the beginning and end of a string, but I couldn't find anything for removing multiple spaces and newlines in a string.
I would like to keep just one whitespace and one newline.
The one-line solution to remove multiple spaces/newlines, but preserve at least one blank line from multiple blank lines:
str = str.replaceAll("(?m)(^ *| +(?= |$))", "").replaceAll("(?m)^$([\r\n]+?)(^$[\r\n]+?^)+", "$1");
Each individual line is trimmed too.
Here's some test code:
String str = " This is\r\n " +
"\r\n" +
" \r\n " +
" \r \n \n " +
"\r\n" +
" a string. ";
str = str.trim().replaceAll("(?m)(^ *| +(?= |$))", "").replaceAll("(?m)^$([\r\n]+?)(^$[\r\n]+?^)+", "$1");
System.out.println(str);
Output:
This is
a string.
The previous advice will trim all whitespace, including the linefeeds and replace them with a single space.
text.replaceAll("\\n\\s*\\n", "\\n").replaceAll("[ \\t\\x0B\\f]+", " ").trim());
First it replaces any instances of linefeeds with only whitespace between them with a single linefeed, then it trims down any other whitespace to a single space ignoring linefeeds.
Here is what I came up with after a bit of testing...
public String keepOneWS(String str) {
Pattern p = Pattern.compile("(\\s+)");
Matcher m = p.matcher(str);
Pattern pBlank = Pattern.compile("[ \t]+");
String newLineReplacement = System.getProperty("line.separator") +
System.getProperty("line.separator");
StringBuffer sb = new StringBuffer();
while (m.find()) {
if(pBlank.matcher(m.group(1)).matches()) {
m.appendReplacement(sb, " ");
} else {
m.appendReplacement(sb, newLineReplacement);
}
}
m.appendTail(sb);
return sb.toString().trim();
}
public void testKeepOneWS() {
String str = " This \t is\r\n " +
"\r\n" +
" \r\n " +
" \r \n \t \n " +
"\r\n" +
" a \t string. \t ";
String expected = "This is" + System.getProperty("line.separator")+
System.getProperty("line.separator") + "a string.";
String actual = keepOneWS(str);
System.out.println("'" + actual + "'");
assertEquals(expected, actual);
}
After a goup of whitespace is captured, it is checked whether it consists only of spaces, if yes then that goup is replaced by one single space, otherwise the goup consits of spaces and line terminators, in this case the group is replaced by one line terminator.
The output is:
'This is
a string.'

java regular expression for multiple spaces

Can I write a single line regular expression instead of the five lines below?
strTestIn = strTestIn.replaceAll("^\\s+", "");
strTestIn = strTestIn.replaceAll("[ ]+", " ");
strTestIn = strTestIn.replaceAll("(\\r\\n)+", "\r\n");
strTestIn = strTestIn.replaceAll("(\\t)+", " ");
strTestIn = strTestIn.replaceAll("\\s+$", "");
What's the difference between these regular expressions?
strTestIn = strTestIn.replaceAll("^\\s+", "");
removes whitespace at the start of the string.
strTestIn = strTestIn.replaceAll("\\s+$", "");
removes whitespace at the end of the string.
strTestIn = strTestIn.replaceAll("[ ]+", " ");
condenses multiple spaces into a single space.
strTestIn =strTestIn.replaceAll("(\\r\\n)+", "\r\n");
removes empty lines by replacing adjacent newlines with a single newline.
strTestIn = strTestIn.replaceAll("(\\t)+", " ");
condenses tabs into a single space.
So they all do different things. A combination is possible for those that have the same replacement string:
strTestIn = strTestIn.replaceAll("^\\s+|\\s+$", "");
strTestIn = strTestIn.replaceAll(" {2,}|\t+", " ");
strTestIn = strTestIn.replaceAll("(\r\n)+", "\r\n");
You can also clean up and improve the regexes a bit (removing some unnecessary backslashes, and changing the minimum number of spaces to two).

Using String's ReplaceAll with regex

How to repalace the following String combination:
word1="word2"
With the following String combination:
word1="word3"
Using word boundaries \b.
I used the following, but did't work:
String word2 = "word2";
String word3 = "word3";
String oldLine = "word1=\"" + word2 + "\"";
String newLine = "word1=\"" + word3 + "\"";
String lineToReplace = "\\b" + oldLine + "\\b";
String changedCont = cont.replaceAll(lineToReplace, newLine);
Where cont is a String that contains a lot of characters including word1="word2" String combinations.
Remove the last \b. It will not do what you think, " is not a word character.
String input = "alma word1=\"word2\"";
String replacement = "word1=\"word3\"";
String output = input.replaceAll("\\bword1=\\\"word2\\\"", replaceMent);
If you replace your lineToReplace line by this:
String lineToReplace = "\\b" + oldLine + "(?!\\w)";
It should work the way you want.
You have word boundaries \b inside your string (the ") and you are using word boundaries in your regexp . Remove that last \b for example.
The only word boundary you need is at the front - the rest of your match already has word boundaries built in (the quotes etc).
This will work:
cont.replaceAll("\\bword1=\"word2\"", "word1=\"word3\"");

removing space before new line in java

i have a space before a new line in a string and cant remove it (in java).
I have tried the following but nothing works:
strToFix = strToFix.trim();
strToFix = strToFix.replace(" \n", "");
strToFix = strToFix.replaceAll("\\s\\n", "");
myString.replaceAll("[ \t]+(\r\n?|\n)", "$1");
replaceAll takes a regular expression as an argument. The [ \t] matches one or more spaces or tabs. The (\r\n?|\n) matches a newline and puts the result in $1.
try this:
strToFix = strToFix.replaceAll(" \\n", "\n");
'\' is a special character in regex, you need to escape it use '\'.
I believe with this one you should try this instead:
strToFix = strToFix.replace(" \\n", "\n");
Edit:
I forgot the escape in my original answer. James.Xu in his answer reminded me.
Are you sure?
String s1 = "hi ";
System.out.println("|" + s1.trim() + "|");
String s2 = "hi \n";
System.out.println("|" + s2.trim() + "|");
prints
|hi|
|hi|
are you sure it is a space what you're trying to remove? You should print string bytes and see if the first byte's value is actually a 32 (decimal) or 20 (hexadecimal).
trim() seems to do what your asking on my system. Here's the code I used, maybe you want to try it on your system:
public class so5488527 {
public static void main(String [] args)
{
String testString1 = "abc \n";
String testString2 = "def \n";
String testString3 = "ghi \n";
String testString4 = "jkl \n";
testString3 = testString3.trim();
System.out.println(testString1);
System.out.println(testString2.trim());
System.out.println(testString3);
System.out.println(testString4.trim());
}
}

Categories