I want to add symbols in between a string variable

Hi guys, hope you can help me here.

Context: I have this string: Factura50004720230301202303153.xlsx
Where:

  • Factura50004720230301202303153.xlsx” is just “Invoice” in spanish and will always be the same
  • “Factura50004720230301202303153.xlsx” is a code for the file and length is dynamic
  • “Factura50004720230301202303153.xlsx” is just a date, (like 2023/03/01) and will always have the same format
  • “Factura50004720230301202303153.xlsx” is simply another date and will always have the same format
  • “Factura50004720230301202303153.xlsx” is just another code for the file (like the 3º version of the file) and length is dynamic

From there I want to get: Factura_500047_20230301_20230315_3.xlsx
So basically I want to separate each of the elements of the string with “_”.

How can I achieve this?
Thank you!

@Gines_Cirera

Assign: inputString = “Factura50004720230301202303153.xlsx”

// Use regular expressions to match the different parts
Assign: match = System.Text.RegularExpressions.Regex.Match(inputString, “^Factura(\d+)(\d{8})(\d{8})(\d+).xlsx$”)

// Extract matched groups
Assign: code = match.Groups(1).Value
Assign: date1 = DateTime.ParseExact(match.Groups(2).Value, “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture).ToString(“yyyyMMdd”)
Assign: date2 = DateTime.ParseExact(match.Groups(3).Value, “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture).ToString(“yyyyMMdd”)
Assign: version = match.Groups(4).Value

// Build the desired output string
Assign: outputString = $“Factura_{code}{date1}{date2}_{version}.xlsx”

Log Message: outputString

1 Like

Hey thank you so much for your answer rlgandu

However I am getting this error when assigning the first date:
Can not assign ‘DateTime.ParseExact(match.Groups(2).Value, “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture).ToString(“yyyyMMdd”)’ to ‘MyString’.

But I guess I can simply do Assign date1: match.Groups(2).Value and the same with the next date, right?

With your Regular Expression the last “code” or “version” has to be 1 digit, can you think of any way to make it dynamic?

Again, thank you for your help.

maybe we simplify it for us when forcing to the current year (can later also be dynamized)


we used groups and replace from regex
[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum

@Gines_Cirera

I can simply do Assign date1: match.Groups(2).Value-Yes,You are correct and datatype is string
the last “code” or “version” has to be 1 digit, can you think of any way to make it dynamic?

Use “Star-[0-infinity]” Instead of “+”

Thank you Peter for your feedback.

2023 is too strict, I guess I could do (20\d{6})(20\d{6}) and use the “20” as starter for the year but, if the first code (the “500047”), contains a “20” followed by 6 digits then everything gets messed up

:+1: perfect grabbed the input and advanced the idea.

We agree to your concerns and it can risk of failures. like this:
grafik

But with some optimistics. When we search for pattern
grafik
and evaluate the matches on its index to calculate the middle ones

we can use:
grafik

Thank you again Peter,

So, as I understand, using that expression I can get:

  • Two matches: both are dates and everything is fine
  • Three matches: the first code has date format and I should only use second and third match.

Is that correct?

Partly:

grafik

When we got 2 items - take it
for all items count > 2 and even ( X Mod 2 = 0 ) - take the middle ones ( partly right )
For all items count Mod 2 = 1 - we cannot predict, sort it out for clearing steps

use index for the _ insertion
grafik

It is absolute not perfect, but we want to support your approach of quality and checking for the side effects like:

I am sorry Peter but I am falling to understand the last case (Mod 2 = 1). Could you explain it in a bit more detail?

Thank you so much and sorry for the trouble.

no problem, with some distance we come to the conclusion:

when matches count = 2 we can rely
when matches count > 2 we do have risks

when match count > 2 and even like 2,4,6, -
grafik
we can pick up the middle ones (but also can be not correct)

when match count > 2 and odd like 3,5,7, -
grafik
we cannot decide as it can be ( for match count 3) CodeLikeDate_Date1_Date2_Code Or Code_Date1_Date2_CodeLikeDate

Ahh okay, I get it now.

Thank you for your help Peter!

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