How to remove only starting and ending special character from a string? - java

I want to remove only the starting special character i.e. " and ending special character ".
The given input string is
String input= "\"This#string%contains^special*characters&.\"";
The expected output is
String output= "This#string%contains^special*characters&.";
Can any one help me how to do this using regex or any other way to remove only starting and ending special character only from the input string in java.

I would use a regex approach:
String input= "\"This#string%contains^special*characters&.\"";
String output = input.replaceAll("^[^A-Za-z0-9]|[^A-Za-z0-9]$", "");
System.out.println(output);
This prints:
This#string%contains^special*characters&.

Related

Merging 2 regex that allow only English and Arabic characters

I have a string and I want to remove any other character such as (0..9!##$%^&*()_., ...) and keep only alphabetic characters.
After looking up and doing some tests, I got 2 regexes formats:
String str = "123hello!#$% مرحبا. ok";
str = str.replaceAll("[^a-zA-Z]", "");
str = str.replaceAll("\\P{InArabic}+", "");
System.out.println(str);
This should return "hello مرحبا ok".
But of course, this will return an empty string because we're removing any non-Latin characters in the first regex then we remove any non-Arabic characters in the second regex.
My question is, how can I merge these 2 regexes in one to keep only Arabic and English characters only.
Use lowercase p since negation is handled with ^ and no quantifier is needed (but wouldn't hurt) since using replaceAll:
String str = "123hello!#$% مرحبا. ok";
str = str.replaceAll("[^a-zA-Z \\p{InArabic}]", "");
System.out.println(str);
Prints:
hello مرحبا ok
Note based on your expected results you want spaces included so a space is in the character list.

string.replaceAll("\\n","") not working if string is taken as input from console and contains newline character

Case 1: Taking string input from scanner and replacing \n with -- (Not working)
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\n", "--");
System.out.println(str);
input: "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n"
Case2: Same thing works if I directly assign string with same value as above.
String str = "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n";
str = str.replaceAll("\n", "--");
PS: I have already tried using \n, line.separater
String str = "Input\\nwith backslash and n";
str = str.replaceAll("\\\\n", "--");
System.out.println(str);
Output:
Input--with backslash and n
We need to escape the backslash twice: To tell the regular expression that a literal backslash is intended we need to put two backslashes. And to tell the Java compiler that we intend literal backslashes in the string, each of those two needs to be entered as two backslashes. So we end up typing four of them.
nextLine() reads one line, so the line cannot contain a newline character. So I have assumes that you were entering a backslash and an n as part of your input.
Less confusing solution
We don’t need to use any regular expression here, and doing that complicates the escaping business. So don’t.
String str = "Input\\nwith backslash\\nand n\\n";
str = str.replace("\\n", "--");
System.out.println(str);
Input--with backslash--and n--
The replace method replaces all occurrences of the literal string given (in spite of not having All in the method name). So now we only need one escape, the one for the Java compiler.
In regular expression if you use single backward slash “\” throws error as it is a escape character. If you use double backward slash “\”, it throws “java.util.regex.PatternSyntaxException: Unexpected internal error near index” exception.
The double backward slash is treated as a single backward slash “\” in regular expression. So four backward slash “\\” should be added to match a single backward slash in a String.
Please try replacing \n with \\n:
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\\\\n", "--");
System.out.println(str);

Java pattern regex with escape characters

I want to replace ";" with "\n" except when it's escaped with a leading '\'. I haven't figured out the correct regex.
Here is what I have:
String s = "abc;efg\\;hij;pqr;xyz\\;123"
s.replaceAll("\\[^\\\\];", "\\\\n");
I'd expect the above string to be replaced with "abc\nefg\;hij;pqr;xyz\;123"
Use a negative look behind:
s = s.replaceAll("(?<!\\\\);", "\n");
The expression (?<!\\) (coded as a java string literal "(?<!\\\\)") means "the previous character should not be a backslash"
Test code:
String s = "abc;efg\\;hij;pqr;xyz\\;123";
s = s.replaceAll("(?<!\\\\);", "\n");
System.out.println(s);
Output:
abc
efg\;hij
pqr
xyz\;123

word extraction and splitting using Java regex

I have a string "'GLO', FLO" Now, I want a regex expression that will check each words in the string and if:
-word begins and ends with a single quote, replace single quotes with spaces
-if a comma is encounted between words split both words using space.
so, in the end, I should get GLO FLO.
Any help on how to do this using replaceAll() method on the string?
This regex didn't do it for me : "'([^' ]+)|\\s+'"
public static void displaySplitString(final String str) {
String pattern1 = "^'?(\\w+)'?,\\s+(\\w+)$";
StringTokenizer strTok = new StringTokenizer(str, " , ");
while (strTok.hasMoreTokens()) {
String delim = (strTok.nextToken());
delim.replaceAll(pattern1, "$1$2");
System.out.println(delim);
}
} //in main method displaySplitString("'GLO', FLO");
Here is the snippet that should get you going:
public static void displaySplitString(String str)
{
String pattern1 = "^'?(\\w+)'?(?=\\S)";
str = str.replaceAll(pattern1, " $1 ");
StringTokenizer strTok = new StringTokenizer(str, " , ");
while (strTok.hasMoreTokens())
{
String delim = (strTok.nextToken());
System.out.println(delim);
}
}
Here,
I change str argument declaration as not final (so that we could change the str value inside the method)
I am using the first regex ^'?(\\w+)'?(?=\\S) to remove potential single quotes from around the first word
Since you use a StringTokenizer, just 2 lines inside the while block are enough.
The regex means:
^ - Start looking for the match at the very start of the string
'? - match 0 or 1 single quote
(\\w+) - match and capture 1 or more alphanumeric symbols (we'll refer to them as $1 in the replacement pattern)
'? - match 0 or 1 single quote
(?=\\S) - match only if there is no space after the optional single quote. Perhaps, you can even replace this lookahead with a mere , if you always have it there, after the first word.

java string split based on new line

I have following string
String str="aaaaaaaaa\n\n\nbbbbbbbbbbb\n \n";
I want to break it on \n so at the end i should two string aaaaaaaa and bbbbbbbb. I dont want last one as it only contain white space. so if i split it based on new line character using str.split() final array should have two entry only.
I tried below:
String str="aaaaaaaaa\n\n\nbbbbbbbbbbb\n \n".replaceAll("\\s+", " ");
String[] split = str.split("\n+");
it ignore all \n and give single string aaaaaaaaaa bbbbbbbb.
Delete the call to replaceAll(), which is removing the newlines too. Just this will do:
String[] split = str.split("\n\\s*");
This will not split on just spaces - the split must start at a newline (followed by optional further whitespace).
Here's some test code using your sample input with edge case enhancement:
String str = "aaaaaaaaa\nbbbbbb bbbbb\n \n";
String[] split = str.split("\n\\s*");
System.out.println(Arrays.toString(split));
Output:
[aaaaaaaaa, bbbbbb bbbbb]
This should do the trick:
String str="aaaaaaaaa\n\n\nbbbbbbbbbbb\n \n";
String[] lines = str.split("\\s*\n\\s*");
It will also remove all trailing and leading whitespace from all lines.
The \ns are removed by your first statement: \s matches \n

Categories

Resources