I'm trying to delete all the spaces and tabs at the end of all my lines.
I use following methods:
string.replace("\\s+$", "");
here, "\s+$" is a regex expression
It seems it' right. However the fact is I would not delete it.
The string is:
a) AAAAAA.
b) BBBBB.
c) CCCCCC.
d) DDDDD.
Like this:
string.replaceAll("\\s+$", "");
String#replace() does not take a regex as argument.
You need to double escape \s
EDIT: yes it does work:
public static void main(final String[] args) {
final String a = " AAAAAAAA ";
System.out.println(a.replaceAll("\\s+$", ""));
}
Maybe your confusion is that you think a.replaceAll() will modify String a. Strings in Java are immutable (they cannot change). a.replaceAll() will return the modified String.
EDIT2: If you're using a multiline String, "\\s+[$\n]" regex should do the work.
Use replaceAll(), which uses regex to find its target, rather than replace(), which uses plain text for its target.
str = str.replaceAll("(?m)\\s+$", "");
Here I've added the (?m) switch that makes carat and dollar match before/after newlines, which you'll need to make it work given the multi-line input.
Delete all the space at end of each line in Java
I guess you need String#trim() method
use
string.trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
Related
I need to replace all the occurrences of a word in a String when it is between non alpha characters(digits, blankspaces...etc) or at the beginning or the end of the String for a $0. However, my Regex pattern does not seem to work when I use replaceAll.
I have tried several solutions which I found on the web, like Pattern.quote, but the pattern doesn't seem to work. However, it works perfectly on https://regexr.com/
public static final String REPLACE_PATTERN = "(?<=^|[^A-Za-z])(%s)(?=[^A-Za-z]|$)";
String patternToReplace = String.format(REPLACE_PATTERN, "a");
inputString = inputString.replaceAll(Pattern.quote(patternToReplace), "$0");
For example, with the string and the word "a":
a car4is a5car
I expect the output to be:
$0 car4is $05car
Just change from inputString.replaceAll(Pattern.quote(patternToReplace), "$0"); to inputString.replaceAll(patternToReplace, "\\$0");
I have tested with this code :
public static final String REPLACE_PATTERN = "(?<=^|[^A-Za-z])(%s)(?=[^A-Za-z]|$)";
String patternToReplace = String.format(REPLACE_PATTERN, "a");
inputString = inputString.replaceAll(patternToReplace, "\\$0");
System.out.println(inputString);
Output :
$0 car4is $05car
Hope this helps you :)
When you want to replace the matching parts of the string with "$0", you have to write it
"\\$0". This is because $0 has a special meaning: The matching string. So you replace the string by itself.
You are quoting the wrong thing. You should not quote the pattern. You should quote "a" - the part of the pattern that should be treated literally.
String patternToReplace = String.format(REPLACE_PATTERN, Pattern.quote("a"));
If you are never going to put anything other letters in the second argument of format, then you don't need to quote at all, because letters do not have special meaning in regex.
Additionally, $ has special meaning when used as the replacement, so you need to escape it:
inputString = inputString.replaceAll(patternToReplace, "\\$0");
Pattern.quote() returns regex literal and everything in-between is treated like a text.
You should use Matcher to replace all string occurrences. Besides that, as #Donat pointed out, $0 is treated like a regex variable, so you need to escape it.
inputString = Pattern.compile(patternToReplace).matcher(inputString).replaceAll("\\$0");
import java.util.StringTokenizer;
class MySplit
{
public static void main(String S[])
{
String settings = "12312$12121";
StringTokenizer splitedArray = new StringTokenizer(settings,"$");
String splitedArray1[] = settings.split("$");
System.out.println(splitedArray1[0]);
while(splitedArray.hasMoreElements())
System.out.println(splitedArray.nextToken().toString());
}
}
In above example if i am splitting string using $, then it is not working fine and if i am splitting with other symbol then it is working fine.
Why it is, if it support only regex expression then why it is working fine for :, ,, ; etc symbols.
$ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the string "$", but as the special meta character $. One sexy solution is:
settings.split(Pattern.quote("$"))
Pattern#quote:
Returns a literal pattern String for the specified String.
... The other solution would be escaping $, by adding \\:
settings.split("\\$")
Important note: It's extremely important to check that you actually got element(s) in the resulted array.
When you do splitedArray1[0], you could get ArrayIndexOutOfBoundsException if there's no $ symbol. I would add:
if (splitedArray1.length == 0) {
// return or do whatever you want
// except accessing the array
}
If you take a look at the Java docs you could see that the split method take a regex as parameter, so you have to write a regular expression not a simple character.
In regex $ has a specific meaning, so you have to escape it this way:
settings.split("\\$");
The problem is that the split(String str) method expects str to be a valid regular expression. The characters you have mentioned are special characters in regular expression syntax and thus perform a special operation.
To make the regular expression engine take them literally, you would need to escape them like so:
.split("\\$")
Thus given this:
String str = "This is 1st string.$This is the second string";
for(String string : str.split("\\$"))
System.out.println(string);
You end up with this:
This is 1st string.
This is the second strin
Dollar symbol $ is a special character in Java regex. You have to escape it so as to get it working like this:
settings.split("\\$");
From the String.split docs:
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.
On a side note:
Have a look at the Pattern class which will give you an idea as to which all characters you need to escape.
Because $ is a special character used in Regular Expressions which indicate the beginning of an expression.
You should escape it using the escape sequence \$ and in case of Java it should be \$
Hope that helps.
Cheers
i have a text with some words like [1], [2], [3] etc...
For example: houses both permanent[1] collections and temporary[2]
exhibitions of contemporary art and photography.[6]
I want to remove these words, so the string must be like this:
For example: houses both permanent collections and temporary
exhibitions of contemporary art and photography.
I tryed using: s = s.replaceAll("[.*]", ""); but it just remove the dots (.) from the text.
Wich is the correct way to achieve it?
thanks
It's because [ and ] are regex markers. This should work:
s = s.replaceAll("\\[\\d+\\]","");
(assuming that you always have numbers within the []).
If it could be any characters:
s = s.replaceAll("\\[.*?\\]","");
(thanks #PeterLawrey).
Use:
s.replaceAll("\\[[^]]+\\]", "")
[ and ] are special in a regular expression and are the delimiters of a character class, you need to escape them. Your original regex was a character class looking either for a dot or a star.
Step 1: get a better (safer) pattern. Your current one will probably remove most of your string, even if you do get it working as written. Aim for as specific as possible. This one should do (only match brackets that have digits between them).
[\d+]
Step 2: escape special regex characters. [] has a special meaning in regex syntax (character classes) so they need escaping.
\[\d+\]
Step 3: escape for string literal. \ has a special meaning in string literals (escape character) so they also need escaping.
"\\[\\d+\\]"
And now we should have some nicely working code.
s = s.replaceAll("\\[\\d+\\]", "");
Try:
public class StringTest {
public static void main(String args[]){
String str = "houses both permanent[1] collections and temporary[2] exhibitions of contemporary art and photography.[6]";
String patten = str.replaceAll("\\[[0-9]*]", "");
System.out.println(patten);
}
}
output:
houses both permanent collections and temporary exhibitions of contemporary art and photography.
Having a hard time replacing a quote with an escaped quote. I have a string that has the value of 'Foo "bar" foobar', and I am trying to replace the quotes around bar with escaped quotes, and it isn't working. I am going crazy.
s=s.replaceAll("\"","\\\"");
I would expect s to have the value of 'foo \"bar\" foobar', but it doesn't. Any help?
replaceAll uses regular expressions - in which the backslash just escapes the next character, even in the replacement.
Use replace instead and it's fine... or you can double the backslash in the second argument if you want to use the regular expression form:
String after = before.replaceAll("\"", "\\\\\"");
This could be useful if you need to match the input using regular expressions, but I'd strongly recommend using the non-regex form unless you actually need regex behaviour.
Personally I think it was a mistake for methods in String to use regular expressions to start with - things like foo.split(".") which will split on every character rather than on periods, completely unexpectedly to the unwary developer.
Use s = s.replace("\"", "\\\"");, which is the non-regex version.
You need to escape both replace all arguments because they are regex!
#Test
public void testReplace() {
assertEquals("foo\\\"bar\\\"",
"foo\"bar\"".replaceAll("\\\"", "\\\\\\\""));
}
So if you want to write " you need to escape the regex -> \" but because you are in java you need to escape the \ and " for java to, so you get \\\\". (that is the search parameter).
For the replace parameter you want to have \" -> in regex: \\\" -> in Java \\\\\\\"
s=s.replaceAll("\"","\\\\\"");
But you should really go with replace(...) as the others here advice.
Use;
s=s.replaceAll("\"","\\\\\"");
String str = "'Foo \"Bar\" Bar'";
System.out.println(str);
System.out.println(str.replaceAll("\"", "\\\\\""));
please follow below program and use > s.replace("/"."//");
public class stringbackslash {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="\";
s=s.replace("\", "\\");
System.out.println(s);
}
}
I am looking to replace \n with \\n but so far my regex attempts are not working (Really it is any \ by itself, \n just happens to be the use case I have in the data).
What I need is something along the lines of:
any-non-\ followed by \ followed by any-non-\
Ultimately I'll be passing the regex to java.lang.String.replaceAll so a regex formatted for that would be great, but I can probably translate another style regex into what I need.
For example I after this program to print out "true"...
public class Main
{
public static void main(String[] args)
{
final String original;
final String altered;
final String expected;
original = "hello\nworld";
expected = "hello\\nworld";
altered = original.replaceAll("([^\\\\])\\\\([^\\\\])", "$1\\\\$2");
System.out.println(altered.equals(expected));
}
}
using this does work:
altered = original.replaceAll("\\n", "\\\\n");
The string should be
"[^\\\\]\\\\[^\\\\]"
You have to quadruple backslashes in a String constant that's meant for a regex; if you only doubled them, they would be escaped for the String but not for the regex.
So the actual code would be
myString = myString.replaceAll("([^\\\\])\\\\([^\\\\])", "$1\\\\$2");
Note that in the replacement, a quadruple backslash is now interpreted as two backslashes rather than one, since the regex engine is not parsing it. Edit: Actually, the regex engine does parse it since it has to check for the backreferences.
Edit: The above was assuming that there was a literal \n in the input string, which is represented in a string literal as "\\n". Since it apparently has a newline instead (represented as "\n"), the correct substitution would be
myString = myString.replaceAll("\\n", "\\\\n");
This must be repeated for any other special characters (\t, \r, \0, \\, etc.). As above, the replacement string looks exactly like the regex string but isn't.
So whenever there is 1 backslash, you want 2, but if there is 2, 3 or 4... in a row, leave them alone?
you want to replace
(?<=[^\\])\\(?!\\+)([^\\])
with
\\$1
That changes the string
hello\nworld and hello\\nworld and hello\\\nworld
into
hello\\nworld and hello\\nworld and hello\\\nworld
I don't know exactly what you need it for, but you could have a look at StringEscapeUtils from Commons Lang. They have plenty of methods doing things like that, and if you don't find exactly what you're searching for, you could have a look at the source to find inspiration :)
Whats wrong with using altered = original.replaceAll("\\n", "\\\\n"); ? That's exactly what i would have done.