Replacing special characters in Java String [duplicate] - java

This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
What is the difference between square brackets and parentheses in a regex?
(3 answers)
Closed 2 years ago.
I want to replace all special characters in the string shown below:
String a="Test’‵"
I want to replace ’ and ‵ with dashes (-). I have tried the following:
a=a.replaceAll("[’|‵]", "-");
This generates the following result:
Test------
instead of
Test--
How can I achieve the desired result?

Don't use square brackets, as it represents a set of single characters to match (a character class).
a=a.replaceAll("’|‵", "-");
Demo!

Related

how to trim specific characters off of Strings on Java [duplicate]

This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")

How to replace the end of a string? [duplicate]

This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?
Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string

Regex find digits after String [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?
If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.

Regex to identify more than 1 occurance of underscore consequtively [duplicate]

This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 6 years ago.
I have string in which more than one underscore can be present together.
For example : this_is__Dummy_____String
I have to replace this more than one occurance by only one underscore so that target string should look like :
this_is_Dummy_String
Thanks in advance !
You can use String#replaceAll to replace the undescores.
"this_is__Dummy_____String".replaceAll("_{2,}", "_")
The given regex will replace all occurences of "two or more" underscores with a single underscore.

Replace dashes with underscores within Any type of bracket [duplicate]

This question already has answers here:
How to replace dashes with underscores within the square brackets using regex Java
(5 answers)
Closed 7 years ago.
I want to replace dashes with underscores within any bracket in a string.
Example String:
[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]
replaceAll=?
output
[a]-[a_gamma]+(a_alpha)*{a}-{b_gamma}+[a]
Try to do this one using lookbehind mechanism in regexp
String input = "[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]";
String result = input.replaceAll("-(?![\\[\\{\\(])","_");

Categories

Resources