Get number from string with regular expression in java
2008-07-17 13:16
615 查看
in java, we use Pattern to compile a regular expression and do the match with Matcher.
Example1:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]){
String str = "12 gb";
pickNumFromString(str);
}
private static void pickNumFromString(String str){
String regEx = "//D+";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
System.out.println(mat.replaceAll(""));
}
}
Or, simply:
Example2:
private static void pickNumFromString(String str){
String regEx = "//D+";
return str.replaceAll(regEx, "");
}
}
Example1:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]){
String str = "12 gb";
pickNumFromString(str);
}
private static void pickNumFromString(String str){
String regEx = "//D+";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
System.out.println(mat.replaceAll(""));
}
}
Or, simply:
Example2:
private static void pickNumFromString(String str){
String regEx = "//D+";
return str.replaceAll(regEx, "");
}
}
相关文章推荐
- How can I get a String from HID device in Python with evdev?
- java,string,startWith or prefix predicate,in c++,for copy
- get number from the string
- Use regular expression to get filename from a file full path name.
- java读写excel时出现Cannot get a NUMERIC value from a STRING cell的解决方法
- DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.NoSuchMethodE
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- leetcode oj java Number of Segments in a String
- How to get file extension from string in C++
- 经典面试题, Exception in thread "main" java.lang.NumberFormatException: input string: ""
- How to convert from string to datetime in sql server? - Sealyu - BlogJava
- get string from assemble in .NET
- Problems with System.OutOfMemoryException At System.String.GetStringForStringBuilder in 32-Bit Managed Solutions
- Get ColumnLetter by ColumnNumber in Excel with VBA
- LeetCode – Regular Expression Matching in Java
- Exception in thread “main” com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
- How to get array from string contained identical symbols in Ruby?
- use regular expression check whether a string is a number
- Problems with System.OutOfMemoryException At System.String.GetStringForStringBuilder in 32-Bit Managed Solutions
- Convert IP addresses from a dots-and-number string to a struct in_addr and back