There's probably a more concise regex, but this will certainly work:
string.replaceAll("[^a-zA-Z0-9]", "");
Answer from stevevls on Stack OverflowThere's probably a more concise regex, but this will certainly work:
string.replaceAll("[^a-zA-Z0-9]", "");
string.replaceAll("[^a-zA-Z0-9]+", "");
Try this code:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str will contain "12.334.78".
I would use a regex.
String text = "-jaskdh2367sd.27askjdfh23";
String digits = text.replaceAll("[^0-9.]", "");
System.out.println(digits);
prints
2367.2723
You might like to keep - as well for negative numbers.
You can use the String.replaceAll method with a regular expression like this:
input.replaceAll("-?[^\\d]", "");
Adding a . will allow decimal:
input.replaceAll("-?[^\\d.]", "");
Edited to support negative numbers.
use a regular expression :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyClass {
public static void main(String args[]) {
String str = "asdjnk -#+-+1m245.g34.34";
System.out.println("Input : " + str);
double result = Double.parseDouble(extractNumber(str));
System.out.println("Final result: " + result);
}
public static String extractNumber(String input){
//replace all characters except digits, +-.
String regex = "[^-+0-9.]";
input = input.replaceAll(regex, "");
System.out.println("After remove non-digit characters: " + input);
/*
number format:
[-]? : if there is 0 or 1 minus sign
[0-9]+ : one or more digits
[.]{1}[0-9]+ : . followed by one or more digits
*/
regex = "[-]?[0-9]+([.]{1}[0-9]+)?";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(input);
if(matcher.find())
return (matcher.group(0));
else
return "error, no numbers exists!";
}
}
result:
Input : asdjnk -#+-+1m245.g34.34
After remove non-digit characters: -+-+1245.34.34
Final result: 1245.34
Sure, add the additional characters (ie. "-") to keep to the character class of things to keep, which is already created and used.
At the end of the character class the "-" means itself (although it could also be escaped). Thus the matching pattern becomes:
"[^a-zA-Z0-9-]"
(This says, match - to remove - everything that is not an English letter, a decimal digit, or a dash.)
You could try
stringOne.replaceAll("^[a-zA-Z0-9-]",""):
Use this site to play around with regex and see if your expression is correct:
http://www.regexplanet.com/advanced/java/index.html
Edit: ^ [a-zA-Z0-9[-]] was incorrect because the two sets are not inclusive. They should be represented as one set of characters: [a-zA-Z0-9-]
Could you try this one?
System.out.println("ja.v_,a".replaceAll("[^a-zA-Z]", "")) //java
I would like to refer to this article and quote it:
Regex examples and tutorials always give you the [a-zA-Z0-9]+ regex to "validate alphanumeric input". It is built-in in many validation frameworks. And it is so utterly wrong. This is a regex that must never appear anywhere in your code, unless you have a pretty good explanation. Yet, the example is ubiquitous. Instead, the right regex is [\p{L}0-9]+
So in your case it would be:
str.replaceAll("[^\\p{L}]", "");
System.out.println("ja.v_,a".replaceAll("[^\\p{L}]", ""));
System.out.println("сл-=о-_=во!".replaceAll("[^\\p{L}]", ""));
Where \p{L} is the Unicode definition of a "letter".