正则表达式:在php中匹配单词后获取一个数字

时间:2015-09-10 05:57:14

标签: php regex

从字符串中获取Bill no(从tesseract OCR输出)

Tesseract OCR字符串如下

1;FTC013233

259139 Bill Date 23/06/2015
Mrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e

Bill No 34939
Hospital ' Req No HG-4 1142645

3;HASH'KA'-A- D 9 %

  Eergncy

VH)

a.. . !‘:‘u"‘_‘i"

Total Amount:

Paid Amount :

2 个答案:

答案 0 :(得分:1)

您可以使用正则表达式

/Bill\s+No\s+(\d+)/ig

<强> Test here

PHP代码:

<?php
$re = "/Bill\\s+No\\s+(\\d+)/i"; 
$str = "1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34939\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n  Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34930999\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n  Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :"; 

preg_match_all($re, $str, $matches);

print_r($matches);
?>

答案 1 :(得分:1)

您可以将preg_matchPositive Lookbehind正则表达式一起用作

preg_match('/(?<=Bill\sNo\s)(\d+)\b/',$str,$res);
echo $res[0];//34939