I have a String like "§aTest§5This§rIs§tA§2Test". I want to use replaceAll to replace every instance of §x, with x, where x means any character.
Is there a quick way to do this?
public static String stringCleanup(final String aString) {
return aString.replaceAll("§(.)", "$1");
}
You can even generalize the above answer to accept an argument with the characters you want to remove.
Related
I need to remove ^A from an incoming string, I'm looking for a regex pattern for it
Don’t want to use \\p{cntrl} , I don’t want to delete the other control characters coming in the string
You should use escaping for '^A':
public static void main(String[] args) {
String value = "remove special char ^A A and B";
System.out.println(value.replaceAll("\\^A", ""));
}
Output:
remove special char A and B
I suggest you avoid using regex and instead use basic string manipulation :
String toRemove = "^A";
yourString.replace(toRemove, "");
You can try it here.
Be careful not to use String methods that work on regexs, especially since the name of the methods aren't very informative (replace replaces all occurences of a fixed string, replaceAll replaces all occurences that match a regex pattern).
program test
so I have a program that will be given two strings as arguments: base and remove. It has to take out remove from base. I am using the split method to remove the second string from the first. However, the case on the chars of the second string should not matter, while the case of the first string must be preserved. I found a lot of ways to do this when you define the remove string in the program as a String literal: I can use ?i for example, but because remove is coming in as an argument and not a literal that I am defining in the program, this isn't working. Please check my attached picture so that you can see exactly where this is failing. Appreciate the help.
public String withoutString(String base, String remove) {
String array[] = base.split(remove);
String result = "";
for(int i = 0; i<array.length;i++)
result+= array[i];
return result;
}
Use a regex pattern with CASE_INSENSITIVE and LITERAL flags:
return Pattern.compile(remove, Pattern.CASE_INSENSITIVE | Pattern.LITERAL)
.matcher(base).replaceAll("");
String.replaceAll() method accepts regex you can use directly like:
return base.replaceAll("(?i)(" + remove +")[\s]*", "")
I'm trying to implement a method that checks a String for different things:
Firstly, the String has to look something like this:
image/<subtype>.
This means, the substring from the beginning until the 5th char has to be image/.
Also the <subtype> can only have 127 chars max. This can be tested by checking the complete String for a length no longer than 133 (as image/ has 6 chars).
Now the substring from 6th char until the end of the complete String can be tested. It is only allowed to contain "a-z", "0-9" "-" and ".".
My method looks like this at the moment:
public static String requireValidImageMimeType(String mimeType){
if(mimeType.length() > 133){
throw new IllegalArgumentException();
}
if (mimeType.startsWith("image/")) {
String mimeTypeToTest = mimeType.substring(6, mimeType.length());
//TODO Create Regex Pattern
if (mimeTypeToTest.matches("^[a-z0-9]+.+-")){
return mimeType;
}
}
return ??;
}
Are there any Regex pros out there?
Replace your ^[a-z0-9]+.+- with ^[-.a-z0-9]+"
Try this regex on match method:
image\\[a-z0-9-.]{0,127}
this should be: "image/anyletterswithmax127chars"
You can do all that in one regex :
^image/[a-z0-9.-]{0,127}$
You can test this regex here
I want to remove "OB" from the string that i put before every vowel.
For example: "THIS IS SOME REALLY GREAT BOLD TEXT" & after adding OB to it: "THOBISOBISSOBOMOBEROBEOBALLYGROBEOBATBOBOLDTOBEXT"
That is the method that i wrote.
public static String unObify(String param) {
String deleteOB = param.replaceAll("[OB]", "");
return deleteOB;
}
Output: THISISSMEREALLYGREATLDTEXT
but the problem is that this method also remove O and B inside my String. and i only want to remove OB which occurs one after the other.
With your current regex [OB], you specify a character class which matches O or B.
If you want to replace OB before every vowel, you could use positive lookahead to assert what follows is a vowel:
OB(?=[AEIOU])
Or as #Tim Biegeleisen pointed out, use make the lookahead case insensitive:
OB(?=(?i)[AEIOU](?-i))
or
OB(?=[aeiouAEIOU])
public static String unObify(String param) {
String deleteOB = param.replaceAll("OB(?=[AEIOU])", "");
return deleteOB;
}
That would replace
THOBISOBISSOBOMOBEROBEOBALLYGROBEOBATBOBOLDTOBEXT
to
THISISSOMEREALLYGREATBOLDTEXT
and
THOBSOBISSOBOMOBEROBEOBALLYGROBEOBATBOBOLDTOBEXT
to
THOBSISSOMEREALLYGREATBOLDTEXT
Remove the [] and write .replaceAll("OB(?=[AaEeIiOoUu])", "");.
[] means match anything inside [] individually
I want to validate a string which donot have numeric characters.
If my string is "javaABC" then it must be validated
If my string is "java1" then it must not be validated
I want to restrict all the integers.
Try this:
String Text = ...;
boolean HasNoNumber = Text.matches("^[^0-9]*$");
'^[^0-9]*$' = From Start(^) to end ($), there are ([...]) only non(^) number(0-9). You can use '\D' as other suggest too ... but this is easy to understand.
See more info here.
You can use this:
\D
"\D" matches non-digit characters.
Here is one way that you can search for a digit in a String:
public boolean isValid(String stringToValidate) {
if(Pattern.compile("[0-9]").matcher(stringToValidate).find()) {
// The string is not valid.
return false;
}
// The string is valid.
return true;
}
More detail is here:
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
The easiest to understand is probably matching for a single digit and if found fail, instead of creating a regexp that makes sure that all characters in the string are non-digits.