I'd like to read through a user input string that removes all nondigit characters excluding some characters like +,-,/,. etc. This is for a calculator. Is there an easy way to do this or do I need to split the initial string beforehand.
            Asked
            
        
        
            Active
            
        
            Viewed 177 times
        
    0
            
            
        - 
                    `String.replaceAll()` takes a [Regex](https://en.m.wikipedia.org/wiki/Regular_expression); write an expression that does what you need. For example `[^0-9+/,.-]`. – Boris the Spider Aug 16 '18 at 18:24
- 
                    You need to provide examples with explicit requirements. So far it seems like a simple regex is fine, with the caveat that it'd likely just be easier to parse it for real, since you'll need to do that anyway to create the calculation part. – Dave Newton Aug 16 '18 at 18:24
- 
                    Possible duplicate of [Java String remove all non numeric characters](https://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters) – wp78de Aug 16 '18 at 19:39
1 Answers
0
            
            
        You can use this. Add any characters you want to keep inside the [ ]
string.replaceAll("[^0-9+-]", "");
When you use ^ inside [], it will match any character that is NOT in [] (excluding the ^).
In this case, it will match any character that is not a number (from 0 to 9), not a +, and not a -.
 
    
    
        No Name
        
- 612
- 6
- 15