Regex condition to match <Currency> <Amount> and also <Amount> <Currency>

Hi,

I am facing a challenge to match an amount and currency combination using regular expressions. It can be the Currency(3 capital letters) followed by Amount(numbers, commas and dots) or Amount followed by Currency.
Samples -
USD 1,000.00
99,766.76 EUR

Both of the above values should be matched. I am able to match both of them separately, but not using a single regex condition. Any help of this would be really helpful

Hi,

Could please use this regex expression,

System.Text.RegularExpressions.Regex.Match(“Your String”,“[A-Z]+\s\d+,\d+.\d+|\d+,\d+.\d+\s\w+”).Value

IThank you.

Hi,

How about the following pattern?

[A-Z]{3} [\d.,]+|[\d.,]+ [A-Z]{3}

image

Thanks @rlgandu , but this regex is matching only USD 1,000 and not 1,000 USD

Thanks @Yoichi , I am able to match both the formats. I actually have to split the text into currency and amount group in a single regex.

I had something like (?<CCY>\w{3})\s*(?<AMT>[0-9.,]*) to retrieve the currency and amount as a separate group. I don’t want to make too many changes to the script to implement this change. I am looking for a single regex condition that could get me the CCY and AMT for the above 2 examples.

Please note that only either one of the format will be available, i.e either USD 1,000.0 or 1,000.00 USD and not both. But the regex should be applicable for both the format

Hi,

How about the following?

m = System.Text.RegularExpressions.Regex.Match(yourString,"(?<CCYY>[A-Z]{3}) (?<AMT>[\d.,]+)|(?<CCY>[\d.,]+) (?<AMT>[A-Z]{3})")

Then

m.Value
m.Groups("CCY").Value
m.Groups("AMT").Value

Sequence.xaml (6.2 KB)

Regards,

Thanks @Yoichi , This is very close to what I need. The problem is, I am not able to use the same group name for currency and amount for the 2 different formats. I had to use different group names for Currency (CCY and CCY2). Is there a possibility to have only one group name CCY and AMT to get the matches for the above 2 formats?

Hi,

Sorry but can you share some examples you wan to achieve? (input and expected output)

Regards,

To put it straight, my regex condition should have only 2 group names CCY and AMT instead of 4 to match the Currency/Amount Format.
example of what I ideally need -
(?<CCY>[A-Z]{3}) (?<AMT>[\d.,]+)|(?<CCY>[\d.,]+) (?<AMT>[A-Z]{3})
But this will not work as the group names cannot be repeated in Regex conditions. Is there any conditional way to specify the group?

For example - if the next 3 are [A-Z] use group <CCY> or if the characters until the next space are [0-9.,], use group <AMT> ?

Hi,

Sorry but i had mistake in the above expression. The following will work. Can you try the following xaml?

m = System.Text.RegularExpressions.Regex.Match(CurrentItem,"(?<CCY>[A-Z]{3}) (?<AMT>[\d.,]+)|(?<AMT>[\d.,]+) (?<CCY>[A-Z]{3})")

Sequence.xaml (7.0 KB)

Regards,

OMG! I apologize. I was so into the Regex101 that I didn’t give it a thought to try in Studio. The Regex101 gave me a pattern error and I thought Regex doesn’t allow duplicate pattern name -

Thank you so much @Yoichi . This is working as I had expected

1 Like

Hi,

FYI, in regex101, it’s necessary to choose .NET(C#) flavor, for UiPath.

Regards,

1 Like

Thanks again @Yoichi , I was frequently getting engine related errors when I was using that, so I switched to other options. But appreciate this note as it could be useful for others.

1 Like

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