我有几个粗略形式的字符串:
[some text] [some number] [some more text]
我想使用Java Regex类提取[一些数字]中的文本。
我大概知道我想使用什么正则表达式(尽管欢迎所有建议)。我真正感兴趣的是Java调用,以获取正则表达式字符串并在源数据上使用它来产生[某个数字]的值。
编辑:我应该补充一点,我只对单个[某些数字]感兴趣(基本上是第一个实例)。源字符串很短,我不打算寻找[一些数字]的多次出现。
我有几个粗略形式的字符串:
[some text] [some number] [some more text]
我想使用Java Regex类提取[一些数字]中的文本。
我大概知道我想使用什么正则表达式(尽管欢迎所有建议)。我真正感兴趣的是Java调用,以获取正则表达式字符串并在源数据上使用它来产生[某个数字]的值。
编辑:我应该补充一点,我只对单个[某些数字]感兴趣(基本上是第一个实例)。源字符串很短,我不打算寻找[一些数字]的多次出现。
当前回答
简单的解决方案
// 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);
其他回答
这个函数从字符串中收集所有匹配的序列。在本例中,它从字符串中获取所有电子邮件地址。
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;
}
对于message = "adf@gmail.com, <another@osiem.osiem>>>> lalala@aaa.pl",它将创建包含3个元素的List。
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
完整的例子:
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)
}
}
由于您正在寻找第一个数字,您可以使用这样的regexp:
^\D+(\d+).*
m.group(1)将返回第一个数字。注意,带符号的数字可以包含负号:
^\D+(-?\d+).*
试着这样做:
Pattern p = Pattern.compile("^.+(\\d+).+");
Matcher m = p.matcher("Testing123Testing");
if (m.find()) {
System.out.println(m.group(1));
}
有时你可以使用java.lang.String中提供的简单的.split("REGEXP")方法。例如:
String input = "first,second,third";
//To retrieve 'first'
input.split(",")[0]
//second
input.split(",")[1]
//third
input.split(",")[2]