Remove unwanted characters from string by regex in Java - java

I have a string here:
javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello
I want to remove everything before the ":", including the ":" itself. This would leave only "Hello". I read about regex, but no combination I tried worked. Can someone tell me how to do it. Thanks in advance!

You need to use replaceAll method or replaceFirst.
string.replaceFirst(".*:\\s*", "");
or
string.replaceAll(".*:\\s*", "");
This would give you only Hello. If you remove \\s* pattern,then it would give you <space>Hello string.
.* Matches any character zero or more times, greedily.
: Upto the colon.
\\s* Matches zero or more space characters.

You could also just split the string by : and take the second string. Like this
String sample = "javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello";
System.out.println(sample.split(":", -1)[1]);
This will output
<space>Hello
If you want to get rid of that leading space just trim it off like
System.out.println(sample.split(":", -1)[1].trim());

Related

Printing Strings in Java using replaceAll

I am a beginner in Java. I don't understand how the below code is able to print all the characters in a string:-
System.out.println(yourString.replaceAll(".", "$0\n"));
I have tried reading the documentation on replaceAll and regex, still no clue.
"." is a regular expression which matches any single character. $0 in the replacement string is a placeholder for the full match of the regex. \n is a line break.
Summarized, this snippet replaces each character with itself and adds a line break after the character.
The syntax for replaceAll() method is as follows:
replaceAll(String regex, Stringreplacement) where:
regex : regular expression
replacement : replacement sequence of characters
so when you what to replace a character with \n basically every character will be printed in a different line. For example: yourString = "Hello." =>
output: Hello with every character on a different line
If the String (as you specified) is String yourString = "-"; so the result of System.out.println(yourString.replaceAll(".", "$0\n")); will be "-\n".
Actually, if you need to print all of the String characters why are you using replaceAll? Coz System.out.println(yourString); will do it perfectly.

How to check and replace a sequence of characters in a String?

Here what the program is expectiong as the output:
if originalString = "CATCATICATAMCATCATGREATCATCAT";
Output should be "I AM GREAT".
The code must find the sequence of characters (CAT in this case), and remove them. Plus, the resulting String must have spaces in between words.
String origString = remixString.replace("CAT", "");
I figured out I have to use String.replace, But what could be the logic for finding out if its not cat and producing the resulting string with spaces in between the words.
First off, you probably want to use the replaceAll method instead, to make sure you replace all occurrences of "CAT" within the String. Then, you want to introduce spaces, so instead of an empty String, replace "CAT" with " " (space).
As pointed out by the comment below, there might be multiple spaces between words - so we use a regular expression to replace multiple instances of "CAT" with a single space. The '+' symbol means "one or more",.
Finally, trim the String to get rid of leading and trailing white space.
remixString.replaceAll("(CAT)+", " ").trim()
You can use replaceAll which accepts a regular expression:
String remixString = "CATCATICATAMCATCATGREATCATCAT";
String origString = remixString.replaceAll("(CAT)+", " ").trim();
Note: the naming of replace and replaceAll is very confusing. They both replace all instances of the matching string; the difference is that replace takes a literal text as an argument, while replaceAll takes a regular expression.
Maybe this will help
String result = remixString.replaceAll("(CAT){1,}", " ");

Remove special characters surrounded by white space

How can i remove special characters having white space on side.
String webcontent = "This is my string. i got this string from blabla.com."
When i use this regex
webcontent.replaceAll("[-.:,+^]*", "");
it becomes like this
String webcontent = "This is my string i got this string from blablacom"
which is not good i want
"This is my string i got this string from blabla.com"
You must test the presence of a white character or the end of the string with a lookahead (?=...) (followed by):
webcontent.replaceAll("[-.?:,+^\\s]+(?:(?=\\s)|$)", "");
The lookahead is only a test and doesn't consume characters.
If you want to do the same with all punctuation characters, you can use the unicode punctuation charcater class: \p{Punct}
webcontent.replaceAll("[\\p{Punct}\\s+^]+(?:(?=\\s)|$)", "");
(note that + and ^ are not punctuation characters.)
You can use negative lookahead to avoid this:
webcontent = webcontent.replaceAll("[-.:?,+^]+(?!\\w)", "");
//=> This is my string i got this string from blabla.com
Try this one
// any one or more special characters followed by space or in the end
// replace with single space
webcontent.replaceAll("[-.:,+]+(\\s|$)", " ").trim();
--EDIT--
if the special character is in the beginning
webcontent.replaceAll("^([-.:,+]+)|[-.:,+]+(\\s|$)", " ").trim();
input:
.This is my string. i got this string from blabla.com.
output:
This is my string i got this string from blabla.com
--EDIT--
I want to replace ? also
webcontent.replaceAll("^([-.:,+]+|\\?+)|([-.:,+]+|\\?+)(\\s|$)", " ").trim();
input
..This is my string.. ?? i got this string from blabla.com..
output
This is my string i got this string from blabla.com
Use the regex [-.:?,+^](\s|$) and remove the character for each match with basic string manipulation. It's a few more lines of code but much, much cleaner.
A pure java solutions where you loop over all special characters and check the next character is also quite simple.
As soon as there are lookaheads/lookbehinds involved, I usually fall back to a non-regex solution for clarity.

Remove repeating set of characters in a string

I want to remove the sequesnce "-~-~-" if it repeats in a string, but only if they are together.
I have tried to create a regex based on the removing of multiple white spaces regex:
test.replaceAll("\\s+", " ");
Unfortunately I was unsuccessful. Can someone please help me write the correct regex? thanks.
Example:
string test = "hello-~-~--~-~--~-~-"
output:
hello-~-~-
Another example
string test = "-~-~--~-~--~-~-hello-~-~--~-~--~-~-"
output:
-~-~-hello-~-~-
The regex is:
test.replaceAll("(-~-~-){2,}", "-~-~-")
replaceAll replaces all occurrences matched by the regex (the first parameter) with the second parameter.
the () groups the expression -~-~- together, {2,} means two or more occurrences.
EDIT
Like #anubhava said, instead of using -~-~- for the replacement string, you could also use $1 which backreferences the first capturing group (i.e. the expression in the regex surrounded by ()).
test.replaceAll("(-~-~-)+", "-~-~-");
This is the regex you need:
(-~-~-){2}

Splitting a string on space except for single space

I was splitting a string on white spaces using the following
myString.split("\\s+");
How do i provide exception for single space. i.e split on space except for single space
Like this:
myString.split("\\s{2,}");
or like this,
myString.split(" \\s+"); // notice the blank at the beginning.
It depends on what you really want, which is not clear by reading the question.
You can check the quantifier syntax in the Pattern class.
You can use a pattern like
myString.split("\\s\\s+");
This only matches if a whitespace character is followed by further whitespace charactes.
Please note that a whitespace character is more than a simple blank.
"Your String".split("\\s{2,}");
will do the job.
For example:
String str = "I am a String";
String []strArr = str.split("\\s{2,}");
This will return an array with length 3.
The following would be the output.
strArr[0] = "I am"
strArr[1] = "a"
strArr[2] = "String"
I hope this answers your question.
If you literally want to exclude a single space, as opposed to other types of whitespace, then you'll need the following:
s.split("\\s{2,}|[\\s&&[^ ]]")
This constructs a character class by subtracting the space from the \s built-in character class.

Categories

Resources