0

i am not familiar with regular expressions maybe one of you can help me. I have a String "46,50 EUR" or "-4.785,20 €" or something similar. I like to remove all chars that are not "0123456789.,-" I tried:

string betrag = "-4.785,20 €"; 
betrag = betrag.replaceAll("/[^0-9.,-]/", "");

but it wont work. The EUR-Sign will not be removed. Maybe it has something to do with the coding? utf-8 vs. latin1? Or my regular expression is wrong?

2 Answers 2

2

Java's regex literals do not require delimiters, so remove the /:

String betrag = "-4.785,20 €"; 
betrag = betrag.replaceAll("[^0-9.,-]+", "");
System.out.println(betrag);  // -4.785,20
Sign up to request clarification or add additional context in comments.

Comments

1

you can also use "/d" - it is shortcut for digital characters but of course the result is the same as mentioned by Tim

String betrag = "-4.785,20 €"; 
betrag = betrag.replaceAll("[^\d.,-]+", ""); 

by the way you can easily test your regular expression via some tools/websites - e.g.: https://www.freeformatter.com/java-regex-tester.html

Comments

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.