Regex to replace everything after a specific symbol? java
replace
doesn't take a regex as parameter but just tries to replace the passed char sequence.
You may use replaceFirst
:
return text.replaceFirst("/.*", "");
(note that there's nothing to escape and no group is necessary)
Java Regex: Remove Everything After Last Instance Of Character
Why use a regex at all here? Look for the last /
character with lastIndexOf
. If it's found, then use substring
to extract everything before it.
How to remove all characters before a specific character in Java?
You can use .substring()
:
String s = "the text=text";
String s1 = s.substring(s.indexOf("=") + 1);
s1.trim();
then s1
contains everything after =
in the original string.
s1.trim()
.trim()
removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
Regex for remove everything after | (with | )
The pipe, |
, is a special-character in regex (meaning "or") and you'll have to escape it with a \
.
Using your current regex:
\|.*$
I've tried this in Notepad++, as you've mentioned, and it appears to work well.
Replace everything after last forward slash
You need to assign the return value of replaceFirst
method to target
as no method in String
class changes the String object itself:
target = target.replaceFirst("[^/]*$", replace);
Use regex java to replace a string before and after a certain character
Your regex did not work because it matched something that is missing from your string:
_
- an underscore followed with....+
- one or more any characters other than a line feed/
- a literal/
symbol[^.]*
- zero or more characters other than a dot/
- a literal/
.
There are no slashes in your input string.
You can use
String myString = "hello_AD123.mp3";
myString = myString.replaceFirst("_.*[.]", ".");
// OR myString = myString.replaceFirst("_[^.]*", "");
System.out.println(myString);
See the IDEONE Java demo
The pattern _[^.]*
matches an underscore and then 0+ characters other than a literal dot. In case the string has dots before .mp3
, "_.*[.]"
matches _
up to the last .
, and needs to be replaced with a .
.
See the regex Demo 1 and Demo 2.
Details:
_
- matches_
[^.]*
- matches zero or more (due to*
quantifier) characters other than (because the negated character class is used, see[^...]
) a literal dot (as.
inside a character class -[...]
- is treated as a literal dot character (full stop, period).- OR
.*[.]
- matches 0 or more characters other than a newline up to the last literal dot (consuming the dot, thus, the replacement pattern should be"."
).
The .replaceFirst()
is used because we only need to perform a single search and replace operation. When the matching substring is matched, it is replaced with an empty string because the replacement pattern is ""
.
How to remove everything after specific character in string using Java
Here is a splitting option:
String input = "analitics@gmail.com@5";
String output = String.join("@", input.split("@")[0], input.split("@")[1]) + "@";
System.out.println(output); // analitics@gmail.com@
Assuming your input would only have two at symbols, you could use a regex replacement here:
String input = "analitics@gmail.com@5";
String output = input.replaceAll("@[^@]*$", "@");
System.out.println(output); // analitics@gmail.com@
Related Topics
Tomcat Is Running But 8080 Port Is Not Responding
What Is the Most Elegant Way to Check If All Values in a Boolean Array Are True
Wait Until All Threads Finish Their Work in Java
Set Current Timezone to @Jsonformat Timezone Value
How to Use Junit to Test Asynchronous Processes
How to Display Pdf Inside Webview in Android
Base64: Java.Lang.Illegalargumentexception: Illegal Character
Java Sorting a 2D Array Rows into Ascending and Columns into Descending Order
How to Replace a Placeholder in a String With a Simpledateformat Pattern
How to Mock a Generic Parameter for a Unit Test in Java
Selecting from Div Class Dropdown - Selenium
How to Return a Select Query from JPA Repository With Foreign Key Type
Get Requestbody and Responsebody At Handlerinterceptor
Passing Multiple Variables in @Requestbody to a Spring MVC Controller Using Ajax
Remove Parentheses, Dashes, and Spaces from Phone Number
Can Not Deserialize Instance of Java.Util.Arraylist Out of Start_Object Token
Could Not Read Json: Can Not Deserialize Instance of Hello.Country[] Out of Start_Object Token