Pattern p = Pattern.compile("(\\D+)(\\d+)(.*)");
Matcher m = p.matcher("this is your number:1234 thank you");
if (m.find()) {
String someNumberStr = m.group(2);
int someNumberInt = Integer.parseInt(someNumberStr);
}
33
在Java 1.4及更高版本中:
String input = "...";
Matcher matcher = Pattern.compile("[^0-9]+([0-9]+)[^0-9]+").matcher(input);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
// if you need this to be an int:
int someNumberInt = Integer.parseInt(someNumberStr);
}
1
看看你可以使用StringTokenizer来做到这一点
String str = "as:"+123+"as:"+234+"as:"+345;
StringTokenizer st = new StringTokenizer(str,"as:");
while(st.hasMoreTokens())
{
String k = st.nextToken(); // you will get first numeric data i.e 123
int kk = Integer.parseInt(k);
System.out.println("k string token in integer " + kk);
String k1 = st.nextToken(); // you will get second numeric data i.e 234
int kk1 = Integer.parseInt(k1);
System.out.println("new string k1 token in integer :" + kk1);
String k2 = st.nextToken(); // you will get third numeric data i.e 345
int kk2 = Integer.parseInt(k2);
System.out.println("k2 string token is in integer : " + kk2);
}
EDIT Allain和Jack提供的表达式建议您需要指定一些非数字子集以捕获数字 . 如果你告诉正则表达式引擎你正在寻找 \d 然后它's going to ignore everything before the digits. If J or A'的表达式适合你的模式,那么整个匹配等于输入字符串 . 并且完全忽略了's no reason to specify it. It probably slows a clean match down, if it isn' t .
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex1 {
public static void main(String[]args) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("hello1234goodboy789very2345");
while(m.find()) {
System.out.println(m.group());
}
}
}
输出:
1234
789
2345
32
简单解决方案
// Regexplanation:
// ^ beginning of line
// \\D+ 1+ non-digit characters
// (\\d+) 1+ digit characters in a capture group
// .* 0+ any character
String regexStr = "^\\D+(\\d+).*";
// Compile the regex String into a Pattern
Pattern p = Pattern.compile(regexStr);
// Create a matcher with the input String
Matcher m = p.matcher(inputStr);
// If we find a match
if (m.find()) {
// Get the String from the first capture group
String someDigits = m.group(1);
// ...do something with someDigits
}
Util类中的解决方案
public class MyUtil {
private static Pattern pattern = Pattern.compile("^\\D+(\\d+).*");
private static Matcher matcher = pattern.matcher("");
// Assumptions: inputStr is a non-null String
public static String extractFirstNumber(String inputStr){
// Reset the matcher with a new input String
matcher.reset(inputStr);
// Check if there's a match
if(matcher.find()){
// Return the number (in the first capture group)
return matcher.group(1);
}else{
// Return some default value, if there is no match
return null;
}
}
}
...
// Use the util function and print out the result
String firstNum = MyUtil.extractFirstNumber("Testing4234Things");
System.out.println(firstNum);
private static final Pattern p = Pattern.compile("^([a-zA-Z]+)([0-9]+)(.*)");
public static void main(String[] args) {
// create matcher for pattern p and given string
Matcher m = p.matcher("Testing123Testing");
// if an occurrence if a pattern was found in a given string...
if (m.find()) {
// ...then you can use group() methods.
System.out.println(m.group(0)); // whole matched expression
System.out.println(m.group(1)); // first expression from round brackets (Testing)
System.out.println(m.group(2)); // second one (123)
System.out.println(m.group(3)); // third one (Testing)
}
}
既然你正在寻找第一个数字,你可以使用这样的正则表达式:
^\D+(\d+).*
和 m.group(1) 将返回第一个数字 . 请注意,带符号的数字可以包含减号:
^\D+(-?\d+).*
6
此函数从字符串中收集所有匹配的序列 . 在此示例中,它从字符串中获取所有电子邮件地址
static final String EMAIL_PATTERN = "[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
public List<String> getAllEmails(String message) {
List<String> result = null;
Matcher matcher = Pattern.compile(EMAIL_PATTERN).matcher(message);
if (matcher.find()) {
result = new ArrayList<String>();
result.add(matcher.group());
while (matcher.find()) {
result.add(matcher.group());
}
}
return result;
}
13 回答
在Java 1.4及更高版本中:
看看你可以使用StringTokenizer来做到这一点
由于我们将这些数值数据转换为三个不同的变量,我们可以在代码中的任何位置使用此数据(以供进一步使用)
除了Pattern之外,Java String类还有几个可以使用正则表达式的方法,在这种情况下代码将是:
其中
\\D
是非数字字符 .Allain基本上有java代码,所以你可以使用它 . 但是,如果您的数字前面只有一个单词字符流,那么他的表达式只会匹配 .
应该能够找到第一个数字串 . 如果你确定它将是第一个数字字符串,你不需要指定它之前的内容 . 同样,除非你想要,否则没有用来指定它之后的内容 . 如果你只是想要这个号码,并且确定它是一个或多个数字的第一个字符串,那么这就是你所需要的 .
如果您希望它被空格偏移,那么它将使指定更加明显
可能会更好 .
如果你需要这三个部分,这将做:
EDIT Allain和Jack提供的表达式建议您需要指定一些非数字子集以捕获数字 . 如果你告诉正则表达式引擎你正在寻找
\d
然后它's going to ignore everything before the digits. If J or A'的表达式适合你的模式,那么整个匹配等于输入字符串 . 并且完全忽略了's no reason to specify it. It probably slows a clean match down, if it isn' t .怎么样
[^\\d]*([0-9]+[\\s]*[.,]{0,1}[\\s]*[0-9]*).*
我认为它会照顾小数部分的数字 . 我包括空格并包含,
作为可能的分隔符 . 我试图从包括浮点数的字符串中获取数字,并考虑到用户可能会犯错并在键入数字时包含空格 .输出:
简单解决方案
Util类中的解决方案
如果您正在阅读文件,那么这可以帮助您
尝试做这样的事情:
有时您可以使用java.lang.String中提供的简单.split("REGEXP")方法 . 例如:
完整示例:
既然你正在寻找第一个数字,你可以使用这样的正则表达式:
和
m.group(1)
将返回第一个数字 . 请注意,带符号的数字可以包含减号:此函数从字符串中收集所有匹配的序列 . 在此示例中,它从字符串中获取所有电子邮件地址
对于
message = "adf@gmail.com, <another@osiem.osiem>>>> lalala@aaa.pl"
,它将创建3个元素的列表 .