【问题标题】:Finding six consecutive integers in three lines of string在三行字符串中查找六个连续整数
【发布时间】:2020-03-18 09:14:18
【问题描述】:

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

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

我想用 Java 8 编写代码,从中可以遍历前三行并获得这 6 个连续数字。

我正在使用 Tesseract 进行 OCR。

例子:

,——— ————i_
g DAILYW RK SHE 278464
E C 0 mp] on THE POUJER Hello, Mumbai, Co. Maha

从中,我需要提取数字278464

请帮忙!!

【问题讨论】:

  • @Lino 6 或更多 所以应该是(\d{6,})
  • 类似这样的 ` String receiptNumber = ""; for (int j = 0; j

标签: java substring ocr


【解决方案1】:

使用正则表达式尝试以下代码。

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{6} 完全匹配 6 个数字
  • (?!\\d) 匹配但不捕获当前文本且当前后不是数字

【讨论】:

  • 由于 OP 正在寻找 6 位或更多位数字,您的正则表达式可以简化,无需检查非数字。查看我对问题的评论
  • 我同意,只要我能支持您的评论,但我还没有足够的声誉。感谢您的帮助 Joakim Danielson。
【解决方案2】:

可以通过\\d{6,}简单解决,如下图:

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;
    }
}

输出:

2784647
278464
278464

【讨论】:

  • 一条注释,在循环之前而不是在循环内部编译模式。
  • @JoakimDanielson - 感谢您的宝贵建议。我已经更新了我的答案以包含它。
【解决方案3】:

检查此代码。

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();
    }
}

}

【讨论】:

  • 使用try(...) 安全地关闭资源。此外,您不需要将整个模式放入(..),第 0 组将始终包含整个匹配项。此外,Scanner 已经是一个模式匹配工具。您忽略了它的功能,只将它用于nextLine(),手动应用Pattern。 →Pattern p = Pattern.compile("\\d{6,}"); public static void main(String[] args) { try(Scanner scanner = new Scanner(new File("your file path"))) { String match; while((match = scanner.findWithinHorizon(p, 0)) != null) { System.out.println(match); } } catch (FileNotFoundException e) { ... } }
猜你喜欢
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多