1

My effort: I tried looking at similar questions however I cannot figure out my answer. I also tried using the web (https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) to figure this out myself, but I just cant get the right answer. Tried using myString.replaceAll("_.+/[^.]*/", "");

I have a string: String myString = "hello_AD123.mp3";

And I want to use regex java in order to REMOVE everything after the underscore (including it) AND stopping before the (.mp3). How would I do this?

So I want the final result to be the following: myString = "hello.mp3";

2
  • 3
    myString = myString.replaceFirst("_[^.]*", ""); Commented Apr 14, 2016 at 14:38
  • 1
    It does remove, I've literally just tried. Commented Apr 14, 2016 at 14:41

1 Answer 1

4

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 "".

Sign up to request clarification or add additional context in comments.

3 Comments

Would you mind explaining the regex you used one by one please? Im confused understanding why it was used like this. It works though! Thanks
OP wants to remove chars between '_' and '.mp3', thus it should rather be like myString.replaceFirst("_.*\\.mp3", "_.mp3"); or (to use matching groups) myString.replaceFirst("(_)(.*)(\\.mp3)", "$1$3");.
@Andy: I see what you mean. Sure, a greedy dot matching pattern can be more useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.