[英]Regex to extract a particular value from the String
在 Rails 控制台中獲取的字符串
Ace Janitorial Service. Inc. Invoice
4901 MORENA BLVD. STE 907
SAN DIEGO, CA 92117 US
(858) 270-7004
rbeckerley@acejanitorialsd.com
www.acejanitorialsd.com
BILL TO
Lab Fellows, Inc.
11011 N Torrey Pines Rd,
La Jolla, Ca 92037
INVOICE # DATE TOTAL DUE DUE DATE TERMS ENCLOSED
31104 02/15/2021 $1,251.92 03/17/2021 Net 30
DATE DESCRIPTION QTY RATE AMOUNT
01/19/2021 Refrigerator Cleaning 2 20.00 40.00
Clean interior of Refrigerators
02/01/2021 Janitorial Services 5,564 0.10 556.40
2x's a week Labs
02/01/2021 Janitorial Services 8,194 0.08 655.52
2x's a week office space
Thank you for your continued business!!
BALANCE DUE $1,251.92
我試過下面的代碼
def extract_invoice_no(msgBody)
pattern = [ "invoice_number", "invoice number", "invoicenumber", "InvoiceNumber", "Invoice number", "Invoice Number", "Invoice_Number", "invoice #", "invoice#", "Invoice #", "Invoice#", "InvoiceNo", "Invoice No", "Invoice no","Invoice_No", "invoice no", "invoice_no", "invoiceno"]
invoiceNo = '';
pattern.each do |each|
if (msgBody.include? each)
invoiceNo = msgBody.split(each)[1].gsub(':','').strip.split(" ")[0]
puts 'invoice no ', invoiceNo
break
end
end
return invoiceNo
end
要求:我需要使用 ruby 代碼從下面的 output 中提取 INVOICE #(Invoice Number)。
Output 獲得日期:日期
Output 需要: 31104
任何幫助都可能對我繼續前進非常有用。
每個模式都以invoice
或Invoice
開頭。
因此,您可以將文本小寫,圍繞/^invoice/
拆分,獲取第二個元素。 然后,您得到第二行,再次拆分,並得到invoice
的第一個“單詞”:
msgBody.downcase.split(/^invoice/)[1].lines[1].split[0]
#=> 31104
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.