如何使用正则表达式查找澳大利亚邮政编码

时间:2018-04-17 17:19:45

标签: ruby-on-rails regex

我正在开发一个RoR项目,我希望从字符串中找到有效的澳大利亚邮政编码。我的字符串如下: -

"[\"\", \"\\\"ParsedText\\\":TVR \", \"PURCHASE \", \"TOTAL \", \"APPROVED \", \"05/02/18 20.51 \", \"TOTAL \", \"EFT \", \"CHANGE \", \"Taxable Iteus \", \"TOTAL includes GST \", \"OTHER SAVINGS \", \"Woolworths \", \"fresh people \", \"VICTORIA PARK PH: (08) 6318 9975 \", \"Your Store Manager is: Mark \", \"Your Assistant Store Manager is: Nick \", \"Your Customer Service Manager is: Taylar \", \"Let us know how can make your shopping trip better \", \"WOOLWORTHS INVOICE - ABN 88 OOO 014 675 \", \"$34.44 \", \"CARO:.............0553 T \", \"$0.00 \", \"$0.75 \", \"22.65 \", \"*SMITHS CRINKLE PERI PERI 150G \", \"SUNRICE BASMATI IKG \", \"NESTLE MILO POUCH 150G \", \"INGHAM BREAST 400G \", \"*BULLA CREAMY CLASSICVANILLA 21 \", \"DON HAM LEG SHAVED ENGLISH BAKEO 250G \", \"GOLDBELGlANCHOCOLATELAVACAKE2PK180G \", \"CHOBANI GREEK YOG BLUEBRY NON FAT 170G \", \"Qty \", \"$2.25 ea \", \"3 \", \"CHOBANI GREEK YOG MANGO FAI I?OG \", \"Qty \", \"$2.25 ea \", \"3 \", \"CHOBANI GREEK YOG BLACK CHERRY 170G \", \"Qty \", \"2 \", \"$2.25 ed \", \"CHOBANI GREEK YOG STRAWB BANANA 170G \", \"Oty \", \"2 \", \"$2.25 ea \", \"CHOBANI GREEK OFFER \", \"17 SUBTOTAL \", \"WOOLWORTHS \", \"VICTORIA PARK \", \"4333 \", \"MERCH ID: 611000606004333 \", \"TERM ID. \", \"\\\"4333088 \", \"Bankwest Credit CREDIT \", \"AIO \", \"A0000000041010 \", \"ARQC \", \"7EB518169AOB56E? \", \"0000000000 \", \"$34.44 \", \"$34.44 \", \"00 \", \"002366 \", \"3.29 \", \"4.20 \", \"2.00 \", \"4. Iff \", \"5.00 \", \"2, BOA \", \"3.00 \", \"6.75 \", \"6.75 \", \"4.50 \", \"4.50 \", \"-12.50 \", \"$34.44 \", \"$34.44 \", \"Thank'S for pi Cking \", \"Total Savings $ 22.65 \", \",\\\"ErrorMessage\\\":\\\"\\\",\", \"\\\"ErrorDetails\\\":\\\"\\\"\", \"},\", \"],\", \"\\\"OCRExitCode\\\":\\\"1\\\",\", \"\\\"IsErroredOnProcessing\\\":\\\"false\\\",\", \"\\\"ErrorMessage\\\":\\\"null\\\",\", \"\\\"ErrorDetails\\\":\\\"null\\\",\", \"\\\"ProcessingTimeInMilliseconds\\\":\\\"1.735\\\",\", \"\\\"SearchablePDFURL\\\":\\\"Searchable PDF not generated as it was not requested.\\\"\", \"}\"]"

我尝试了两种模式: -

mystring[/(0[289][0-9]{2})|(0[89][0-9]{2})|([1-2]{1}[0-9]{1}[0-9]{2})|(2[9][0-1]{1}[1-4]{1})|([3-7]{1}[0-5]{1}[0-9]{2})|([7-9]{1}[8-9]{1}[0-9]{2})/]

mystring[/\d{4}/]

但这两种模式都会返回无效的邮政编码。由于我的字符串包含电话号码(08) 6318 9975,因此它会返回前四个数字6318,这是一个无效的邮政编码。我的字符串中的正确邮政编码是4333。请帮我找到有效的邮政编码。提前谢谢!

1 个答案:

答案 0 :(得分:0)

检查

(?<!\d |\d)\d{4}(?= ?\\)
页面右上角的

example说明

或者

(?<=\")\d{4}(?= ?\\)
页面右上角的

example说明

注意:在ruby中实现转义特殊字符(我不知道Ruby可能是真的可能是假的)

mystring[/(?<!\d |\d)\d{4}(?= ?\\)/]

mystring[/(?<=\")\d{4}(?= ?\\)/]