Java replaceAll method with a variable string and escaped dot - java

I'm having a hard time figuring this one out, so I ask for your help. Here's the deal:
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll("([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)", "$1$2");
The block of code above results in 02-EST-WHATEVER-099-.dwg (removed the last "00", just before the extension). Great, that's what I need!
But the RegEx I use above has to be created on the fly (the field I'm removing can be in a different position). So I used some code to create the RegEx string (here's what the result would look like if I just declared it):
String regexRemoveRev = "([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)";
Now, if I out.print(regexRemoveRev), I get ([^-_\.]+-[^-_\.]+-[^-_\.]+-[^-_\.]+-)[^-_\.]+(\.[^-_\.]+) (notice the single backslashes).
And when i try the replaceAll again, it doesn't work:
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll(regexRemoveRev, "$1$2");
So I thought it could be because of the single backslashes, and I tried declaring regexRemoveRev with 4 of them, instead of just 2:
String regexRemoveRev = "([^-_\\\\.]+-[^-_\\\\.]+-[^-_\\\\.]+-[^-_\\\\.]+-)[^-_\\\\.]+(\\\\.[^-_\\\\.]+)";
The output of out.print(regexRemoveRev) is the double backslash version of the RegEx, as expected:
([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)
But the replace still doesn't work!
How do I get this to do what I want?

I have just wrote a short program and in both cases it works here it is:
public class StringTest
{
public static void main(String[] args)
{
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll("([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)", "$1$2");
String regexRemoveRev = "([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)";
String newStr1 = str.replaceAll(regexRemoveRev, "$1$2");
System.out.println("newStr: "+newStr);
System.out.println("regexRemoveRev: "+regexRemoveRev);
System.out.println("newStr: "+newStr1);
}
}
The out put from the above:
newStr: 02-EST-WHATEVER-099-.dwg
regexRemoveRev: ([^-.]+-[^-.]+-[^-.]+-[^-.]+-)[^-.]+(.[^-.]+)
newStr: 02-EST-WHATEVER-099-.dwg
I am not sure why is not working for you!! or is it something else you are asking and I got wrong

Related

Changing a few char for one the same

I have question about String class in Java.
I want remove every punctuation marks. To be exact I use replace() method and replace all marks for: "";
But my question is can I do it more smoothly? Becouse now I replace every sign separately
String line1 = line.replace(".", "");
String line2 = line1.replace("?", "");
String line3 = line2.replace("!", "");
String line4 = line3.replace("\n", "");
Ok I find helpful and nice solution.
String line11 = line.replaceAll("[\\p{Punct}]", "");
use replaceAll, and reg []
String str = "hellol,lol/,=o/l.o?ll\no,llol";
str = str.replaceAll("[,=/\\n\\?\\.]", "");
System.out.println(str);
If we want to replace every punctuation mark then we can use the replaceAll() method in java to achieve that. replaceAll("[^a-zA-Z ]", "")), This line makes a java compiler to understand every character other than alphabets(both lowercase and uppercase) to be replaced by "" i.e,empty. with this we can replace every punctuation marks in a particular string.
public class HelloWorld {
public static void main(String[] args) {
String line="Th##is i*s a Ex()ample St!#ing!#";
System.out.println(line.replaceAll("[^a-zA-Z ]", ""));
}
}

How to remove backslash("\") from string having ASCII string combination in java

Input string is "ABC\1067546161"
I want to remove "\" backslash and get digits only from the string but we are getting string with ascii value.Result String is ABCF7546161 after print.
Please suggest some solution.
Input String is ABC\1067546161
Expected result is 1067546161
May be something like this
"ABC\1067546161".replaceAll("[a-zA-Z\\]", "")
I think this could work, but the code is ugly as hell..
String word = "ABC\1067546161";
char badChar = word.charAt(3);
String[] arr = word.split(Character.toString(badChar));
System.out.println(Integer.toOctalString(badChar) + arr[1]);
You only mentioned one string in the question, but on several cases, this would most likely not work.
As #TheLostMind pointed out in a comment, you can't replace the backslash directly because the String is created with that value.
The only way to do that is manipulate the input itself and convert it into a byte array instead of a String. Then you can call the String constructor that takes a byte[] as argument and it won't be converted.
Once you have that, you can use a regex to remove the part you don't want as others suggested. Here's the code I've used to test this:
public static void main(String[] args) {
// Input manipulation.
byte[] input = {'A','B','C','\\','1','0','6','7','5','4','6','1','6','1'};
String string = new String(input);
System.out.println(string);
// Splitting.
String[] result = string.split("\\\\");
System.out.println(result[1]);
}

Regex do not change anything

Please have a look at the below code
public class PuctuationRemover {
public PuctuationRemover()
{
String str = ":The red; third.fox is hungry!!! but, is he angry? doesn't! (yeah!). Call 911! system. can't access it! what the , hell . is this. people of my country, really? 123465 can^be,found.... OK . you got it? ";
String str2 = str.replaceAll("[^a-zA-Z'\\s]+", str);
System.out.println(str2);
}
public static void main(String[]args)
{
new PuctuationRemover();
}
}
The expected output is
The red thirdfox is hungry but is he angry doesn't yeah Call system can't access it what the hell is this people of my country really canbefound OK you got it
The output I get is
:The red; third.fox is hungry!!! but, is he angry? doesn't! (yeah!). Call 911! system. can't access it! what the , hell . is this. people of my country, ..............
The original working regex is here.
What has gone wrong here?
If you need to remove the punctuation, supply an empty string as the second argument instead of the original string itself. The second argument to replaceAll is not the original string, but what to replace the match with. Change
String str2 = str.replaceAll("[^a-zA-Z'\\s]+", str);
with
String str2 = str.replaceAll("[^a-zA-Z'\\s]+", "");
Use String str2 = str.replaceAll("[^a-zA-Z'\\s]+", "");
You are doing String str2 = str.replaceAll("[^a-zA-Z'\\s]+", str); .You are replacing the whole original String.
Please see this javadoc for more on String replaceAll.
Here in public String replaceAll(String regex, String replacement) second argument, i.e replacement is the string to be substituted for each match.
Note: Its better to catch PatternSyntaxException .
You're replacing your old string with your old string, which means you keep the same string even after replacing it. What you need to do is replace all of the characters specified in the regex with a no-character "".
EDIT: Darn, you people are fast typers! =P

How to insert backslash into my string in java?

I have string, and I want to replace one of its character with backslash \
I tried the following, but no luck.
engData.replace("'t", "\\'t")
and
engData = engData.replace("'t", String.copyValueOf(new char[]{'\\', 't'}));
INPUT : "can't"
EXPECTED OUTPUT : "can\'t"
Any idea how to do this?
Try this..
String s = "can't";
s = s.replaceAll("'","\\\\'");
System.out.println(s);
out put :
can\'t
This will replace every ' occurences with \' in your string.
Try like this
engData.replace("'", "\\\'");
INPUT : can't
EXPECTED OUTPUT : can\'t
String is immutable in Java. You need to assign back the modified string to itself.
engData = engData.replace("'t", "\\'t"); // assign the modified string back.
This is possible with regex:
engData = engData.replaceAll("('t)","\\\\$1");
The ( and ) specify a group. The 't will match any string containing 't. Finally, the second part replaced such a string with a backslash character: \\\\ (four because this), and the first group: $1. Thus you are replacing any substring 't with \'t
The same thing is possible without regex, what you tried (see this for output):
engData = engData.replace("'t","\\'t"); //note the assignment; Strings are immutable
See String.replace(CharSequence, CharSequence)
For String instances you can use, str.replaceAll() will return a new String with the changes requested:
String str = "./";
String s_modified = s.replaceAll("\\./", "");
The following works for me:
class Foobar {
public static void main(String[] args) {
System.err.println("asd\\'t".replaceAll("\\'t", "\\\'t"));
}
}

How can I remove any caret symbols in a string?

I'm using a regex string that contains a carat (^) symbol somewhere inside of it. Is there a way in Java to remove these symbols? Here are a few methods I've tried:
string = "some^string";
string = string.replaceAll("\\^", "");
string = string.replaceAll(Pattern.quote("\\^"), "");
string = string.replaceAll(Pattern.quote("\u2038"), "");
None of which have worked. What am I missing?
There's no need to use regular expressions at all:
string = string.replace("^", "");
However, the first of your examples works too:
public class Test {
public static void main(String[] args) throws Exception {
String string = "some^string";
string = string.replaceAll("\\^", "");
System.out.println(string); // Prints somestring
}
}
... so it's entirely possible that your problem is elsewhere.
string.replaceAll("\\^", ""); should work.
Pls. delete last 2 lines from above your code and do write below things only then check.
string = "some^string";
string = string.replaceAll("\^", "");
I think you can try using another variable name, like for example:
String str1 = string.replace("^", "1");
and use the new str1, instead of the old string.

Categories