Regex not working for replacing special characters - java

I want to replace special characters with nothing. So i tried
this.name.replace("[^a-zA-Z]+", "").trim()
I wnat the below word to be 000 Vektor
OOO "Vektor"

The documentation of replace says:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
It won't take regular expressions.
The documentation of replaceAll says:
Replaces each substring of this string that matches the given regular expression with the given replacement.
So you may use:
this.name.replaceAll("[^a-zA-Z]+", "").trim();
You may also use replaceFirst with regular expressions, though not here.
Also, in a comment you say that you have tried it. I suspect that it is because you just use :
this.name.replaceAll("[^a-zA-Z]+", "").trim();
But java Strings are immutable, and don't change by themselves.
Hence you should use:
this.name = this.name.replaceAll("[^a-zA-Z]+", "").trim();

String.replace takes a literal first argument. replaceAll uses a regex
name = name.replaceAll("[^a-zA-Z ]+", "");

Related

Why does the following regex not work for Java?

I want to delete a word and all its trailing whitespace.
Here is my regex:
item.getName().replace(word + "(\\s*)?", "");
I tested this statement by running:
item.getName().replace(word, "");
This executes successfully, albeit with extra whitespaces. So the error must be due to "(\\s*)?" part. Is it because I did not escape the slash correctly? Or does Java not recognize something in that regex?
replace uses a String literal as its first argument. Use replaceAll instead
String.replace method does not take regular expressions. I believe you'd have to use replaceAll in orer to use regular expression. Also, regular expressions are a general grammar that expresses a certain pattern of String rather than a particular instances that contain certain words. You can't mix a word with a regular expression such way.

Replace a special character with other special character within string

I want to replace a special character " with \" in string.
I tried str = str.replaceAll("\"","\\\");
But this doesnt work.
The closing quotes are missing in the 2nd parameter. Change to:
str = str.replaceAll("\"","\\\\\"");
Also see this example.
String.replaceAll() API:
Replaces each substring of this string that matches the given regular
expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)
yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special
meaning of these characters, if desired.
Btw, it is duplicated question.
You have to escape the \ by doubling it:\\
Code example:
String tt = "\\\\terte\\";
System.out.println(tt);
System.out.println(tt.replaceAll("\\\\", "|"));
This gives the following output:
\\terte\
||terte|

Match "_<digit>string" wth a regular expression

I have a list of strings like
xxx_2pathway
xxx_6pathway
xxx_pathway
So I have a string followed by an underscore and "pathway". There may be a digit between the underscore and "pathway". How can I match and replace everything except xxx with a regular expression in Java?
This does not work:
pathnameRaw = pathnameRaw.replace("_\\dpathway","");
Your regex is almost fine. Since the digit is optional, add a ? at the end of \\d.
Also the replace method does not use regex. Use replaceAll instead.
See it
"_[0-9]?pathway"

Problem replacing words using [^a-zA-Z] regex

Just could not get this one and googling did not help much either..
First something that I know: Given a string and a regex, how to replace all the occurrences of strings that matches this regular expression by a replacement string ? Use the replaceAll() method in the String class.
Now something that I am unable to do. The regex I have in my code now is [^a-zA-Z] and I know for sure that this regex is definitely going to have a range. Only some more characters might be added to the list. What I need as output in the code below is Worksheet+blah but what I get using replaceAll() is Worksheet++++blah
String homeworkTitle = "Worksheet%#5_blah";
String unwantedCharactersRegex = "[^a-zA-Z]";
String replacementString = "+";
homeworkTitle = homeworkTitle.replaceAll(unwantedCharactersRegex,replacementString);
System.out.println(homeworkTitle);
What is the way to achieve the output that I wish for? Are there any Java methods that I am missing here?
[^a-zA-Z]+
Will do it nicely.
You just need a greedy quantifier in order to match as many non-alphabetical characters you can, and replace the all match by one '+' (a - by default - greedy quantifier)
Note: [^a-zA-Z]+? would make the '+' quantifier lazy, and would have give you the same result than [^a-zA-Z], since it would only have matched only one non-alphabetical character at a time.
String unwantedCharactersRegex = "[^a-zA-Z]"
This matches a single non-letter. So each single non-letter is replaced by a +. You need to say "one or more", so try
String unwantedCharactersRegex = "[^a-zA-Z]+"

String replace function

I have following string
String str = "replace :) :) with some other string";
And I want to replace first occurance of :) with some other string
And I used str.replaceFirst(":)","hi");
it gives following exception
"Unmatched closing ')'"
I tried using replace function but it replaced all occurance of :).
The replaceFirst method takes a regular expression as its first parameter. Since ) is a special character in regular expressions, you must quote it. Try:
str.replaceFirst(":\\)", "hi");
The double backslashes are needed because the double-quoted string also uses backslash as a quote character.
The first argument to replaceFirst() is a regular expression, not just a character sequence. In regular expressions, the parantheses have special significance. You should escape the paranthesis like this:
str = str.replaceFirst(":\\)", "hi");
Apache Jakarta Commons are often the solution for this class of problems. In this case, I would have a look at commons-lang, espacially StringUtils.replaceOnce().

Categories