我有几个粗略形式的字符串:

[some text] [some number] [some more text]

我想使用Java Regex类提取[一些数字]中的文本。

我大概知道我想使用什么正则表达式(尽管欢迎所有建议)。我真正感兴趣的是Java调用,以获取正则表达式字符串并在源数据上使用它来产生[某个数字]的值。

编辑:我应该补充一点,我只对单个[某些数字]感兴趣(基本上是第一个实例)。源字符串很短,我不打算寻找[一些数字]的多次出现。


当前回答

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

其他回答

如何(^ \ \ d) * ([0 - 9] + [\ \ s] * [,] {0,1} \ \ [s] *[0 - 9] *)。*我认为它可以处理小数部分的数字。 我加入了空白和可能的分隔符。 我试图得到的数字从一个字符串,包括浮动,并考虑到用户可能会犯一个错误,并包括空白,而键入的数字。

简单的解决方案

// 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);

除了Pattern之外,Java String类还有几个可以使用正则表达式的方法,在您的情况下,代码将是:

"ab123abc".replaceFirst("\\D*(\\d*).*", "$1")

其中\\D是非数字字符。

如果你正在从文件中读取,那么这可以帮助你

              try{
             InputStream inputStream = (InputStream) mnpMainBean.getUploadedBulk().getInputStream();
             BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
             String line;
             //Ref:03
             while ((line = br.readLine()) != null) {
                if (line.matches("[A-Z],\\d,(\\d*,){2}(\\s*\\d*\\|\\d*:)+")) {
                     String[] splitRecord = line.split(",");
                     //do something
                 }
                 else{
                     br.close();
                     //error
                     return;
                 }
             }
                br.close();

             }
         }
         catch (IOException  ioExpception){
             logger.logDebug("Exception " + ioExpception.getStackTrace());
         }

试着这样做:

Pattern p = Pattern.compile("^.+(\\d+).+");
Matcher m = p.matcher("Testing123Testing");

if (m.find()) {
    System.out.println(m.group(1));
}