Regex Capture an Amount and a String

Hello Fellow RPA Developers,

Is there someone who can help me to capture an amount and a string using REGEX?

These are the transaction rows I want to extract:

GTB: 123456| fee_amount: 165.0 | QR
GTB: 1000000| fee_amount: 24.0 | QR
GTB: 123455| fee_amount: 135.0 | NON-QR
GTB: 1234567| fee_amount: 13.0 | NON-QR
GTB: 100002| fee_amount: 15.0 | QR
GTB: 100004| fee_amount: 15.0 | NON-QR

I want to capture the number after GTB, the fee amount and if it is QR or NON-QR.

Example on first row:
Invoice_Number = 123456, Fee = 165, Scan = QR

If it is possible if it is a line REGEX because I will integrate it inside a LINQ like the image below.
image

Best Regards,
Anthony Jr.

Hi @anthonyjr ,

Could you maybe let us in on some more details ? What does the variable Details contain ?

Is it one row or all data as shown in your post ?

Also, Could you post the Entire Linq Expression that you are using ?

HI,

Can you share final result?

mc = System.Text.RegularExpressions.Regex.Matches(yourString,"GTB\D+(?<INVOICE>\d+)\W+fee_amount:\s*(?<AMOUNT>[\d,.]+)\W+(?<QR>[-\w]+)")

dt = mc.Cast(Of System.Text.RegularExpressions.Match).Select(Function(m) dt.LoadDataRow({m.Groups("INVOICE").Value,m.Groups("AMOUNT").Value,m.Groups("QR").Value},False)).CopyToDataTable

The above returns the following datatable.

image

Sample20230126-5L.zip (7.9 KB)

Regards,

Ahh sorry sir @Yoichi and sir @supermanPunch for the confusion , just disregard the linq and data table part.

I think I just need the regex on each captured value.

These are the two strings:

GTB: 123456| fee_amount: 165.0 | QR

GTB: 1234567| fee_amount: 13.0 | NON-QR

How to extract the numbers after GTB? and the fee amount? and if it is QR or NON-QR?

I will assign the number after GTB as “InvoiceNumber” variable
I will assign the fee amount as “Fee” variable
I will assign the QR or NON-QR as “Scan” variable

Expected output on first string:
InvoiceNumber = “123456”
Fee = “165.0”
Scan = “QR”

Expected output on second string:
InvoiceNumber = “1234567”
Fee = “13.0”
Scan = “NON-QR”

The regex should be the same on the two expected output.

Regards,
Anthony

HI,

The following will work.

m = System.Text.RegularExpressions.Regex.Match(yourString,"GTB\D+(?<INVOICE>\d+)\W+fee_amount:\s*(?<AMOUNT>[\d,.]+)\W+(?<QR>[-\w]+)")

Then

m.Groups("INVOICE").Value
m.Groups("AMOUNT").Value
m.Groups("QR").Value

note: m is System.Text.RegularExpressions.Match type

Regards,

@anthonyjr ,

Alternate Expressions Individually captured :

InvoiceNumber = Regex.Match("GTB: 123456| fee_amount: 165.0 | QR","(?<=GTB:).*?(?=\|)").value.Trim

Fee = Regex.Match("GTB: 123456| fee_amount: 165.0 | QR","(?<=fee_amount:).*?(?=\|)").value.Trim

Scan = Regex.Match("GTB: 123456| fee_amount: 165.0 | QR","(?<=fee_amount:.*\|).*").value.Trim

Visuals :
image

Thank you sir @supermanPunch , I was able to use it inside the LINQ too!

This is sample process I’m expecting
Sample Test.zip (2.9 KB)

	Let details = a("details").ToString
	Let InvNo = Regex.Match(details,"(?<=GTB:).*?(?=\|)").value.Trim
	Let Fee = Regex.Match(details,"(?<=fee_amount:).*?(?=\|)").value.Trim
	Let targetmode = Regex.Match(details,"(?<=fee_amount:.*\|).*").value.Trim

I appreciate the solution you presented sir @Yoichi !

Regards,
Anthony Jr.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.