首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在三行字符串中找到六个连续整数

在三行字符串中找到六个连续整数
EN

Stack Overflow用户
提问于 2020-03-18 09:14:18
回答 3查看 284关注 0票数 1

我用Java编写了一个OCR程序,它扫描文档并找到其中的所有文本。我的主要任务是找到发票号码,可以是6或更多的整数。

我使用了子字符串功能,但效率不高,因为该数字的位置随每个文档的变化而变化,但它总是出现在OCR文本的前三行中。

我想用Java 8编写代码,在这里我可以迭代前三行,并得到这6个连续的数字。

我正在使用Tesseract作为OCR。

示例:

代码语言:javascript
复制
,——— ————i_
g DAILYW RK SHE 278464
E C 0 mp] on THE POUJER Hello, Mumbai, Co. Maha

从这里,我需要提取数字278464

救命啊!!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-03-18 09:46:32

使用regex尝试以下代码。

代码语言:javascript
复制
import java.lang.Math; // headers MUST be above the first class
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    Pattern pattern = Pattern.compile("(?<=\\D)\\d{6}(?!\\d)");
    String str = "g DAILYW RK SHE 278464";
    Matcher matcher = pattern.matcher(str);
    if(matcher.find()){
        String s = matcher.group();
        //278464
        System.out.println(s);
    }
  }
}

  • (?<=\D)匹配但不捕获文本当前,在当前之前不匹配数字
  • \d{6}准确匹配6个数字
  • (?!\d)匹配但不捕获文本当前和当前之后不是数字
票数 1
EN

Stack Overflow用户

发布于 2020-03-18 10:05:46

可以简单地使用\\d{6,}来解决这个问题,如下所示:

代码语言:javascript
复制
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String args[]) {
        // Tests
        String[] textArr1 = { ",——— ————i_", "g DAILYW RK SHE 2784647",
                "E C 0 mp] on THE POUJER Hello, Mumbai, Co. Maha" };
        String[] textArr2 = { ",——— ————i_", "g DAILYW RK SHE ——— ————",
                "E C 0 mp] on THE 278464 POUJER Hello, Mumbai, Co. Maha" };
        String[] textArr3 = { ",——— 278464————i_", "g DAILYW RK SHE POUJER",
                "E C 0 mp] on THE POUJER Hello, Mumbai, Co. Maha" };
        System.out.println(getInvoiceNumber(textArr1));
        System.out.println(getInvoiceNumber(textArr2));
        System.out.println(getInvoiceNumber(textArr3));
    }

    static String getInvoiceNumber(String[] textArr) {
        String invoiceNumber = "";
        Pattern pattern = Pattern.compile("\\d{6,}");
        for (String text : textArr) {    
            Matcher matcher = pattern.matcher(text);
            if (matcher.find()) {
                invoiceNumber = matcher.group();
            }
        }
        return invoiceNumber;
    }
}

输出:

代码语言:javascript
复制
2784647
278464
278464
票数 1
EN

Stack Overflow用户

发布于 2020-03-18 10:19:50

检查这段代码。

代码语言:javascript
复制
public class Test {

private static final Pattern p = Pattern.compile("(\\d{6,})");

public static void main(String[] args) {
    try {
        Scanner scanner = new Scanner(new File("here put your file path"));
        System.out.println("done");
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            // create matcher for pattern p and given string
            Matcher m = p.matcher(line);
            // if an occurrence if a pattern was found in a given string...
            if (m.find()) {
                System.out.println(m.group(1)); // second matched digits
            }
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60736449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档