【问题标题】:Give me regex for finding account number in bunch of converted text documents from bank statement pdf给我正则表达式,以便在银行对帐单 pdf 中转换的文本文档中查找帐号
【发布时间】:2020-09-01 12:19:33
【问题描述】:

我有一堆不同的银行对账单 pdf,我已将它们转换为要从中提取消费者信息的文本。我必须编写正则表达式来提取帐号。帐号后面是特定的关键字,例如:

account number,
account no,
a/c no

我将粘贴一些包含此信息的示例文本。

样本 1:

"bank of india  account statement name abcd account no. 123456 account type savings account"

示例 2:

"statement for a/c no 11111111 between 16-09-2019 and 16-03-2020"

示例 3:

"shyam alaspure<br />
period<br />
01-12-2019 to 29-02-2020<br />
cust.reln.no<br />
XXXXXXXX<br />
account no<br />
9XXX99999"<br />

我使用了以下正则表达式

'account no.\s*([^.]+|\S+)'

但输出给出了帐号之后的所有文本。

请帮我找出解决办法。

【问题讨论】:

  • 得到答案正则表达式应该是r'account no.\s+((?:\w+(?:|$)){1})'
  • 所以,你拥有的是account no.\s+(\w+)。但看起来它只匹配第一个输入。

标签: python python-3.x regex pandas pdftotext


【解决方案1】:

注意

  • (?:|$) 总是匹配一个空字符串,这个模式是多余的
  • {1} 总是多余的,永远不要在手动编写的正则表达式中使用它
  • 在字符类之外使用的. 匹配除换行符之外的任何字符,始终对其进行转义,\.,以匹配文字字符。 [.] 也匹配文字点。

你可以使用

r'\b(?:a/c|account) no\.?\s+(\w+)'

regex demo

详情

  • \b - 单词边界
  • (?:a/c|account) - a/caccount
  • no.? - 空格,no,可选点
  • \s+ - 1+ 个空格
  • (\w+) - 第 1 组:一个或多个字母、数字或下划线

【讨论】:

  • 在此之前非常感谢我将模式保存在列表中并应用于文本。
【解决方案2】:

请使用下面的正则表达式

(?i)(?:a\/c|account)\s*no\.?\s+(\w+)

看到它在工作here

【讨论】:

  • 这是我的正则表达式建议,无需复制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 2016-09-18
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多