How to form regex for a dynamic string

I want to form a regex to get the following particular string from list of strings.
ITNddMM1.zip + anything. all that we should be concerned about is the part till zip.
this is required to filter out mails from the list of mails and then download the same.

Hi @Suraj_Das_IN

Welcome to the UiPath community.

please try this

ITNddMM1.zip.*

or this if it has multiple lines,

ITNddMM1.zip[\d\D\w\W\n\s.]*

If ITNddMM1 is not constant try this,

.*.zip.*

Or

.*.zip[\d\D\w\W\n\s.]*

Pick the pattern according to your need, all of them works with your requirement.

Thanks

1 Like

let me try, but how should i use this in assign activity like any specific function to enter the pattern as input to it ?

Did you find the activity called matches.

If you not find try this in assign,

Result = system.text.regularexpression.regex.matches(yourstring,yourpattern)

Result - System.Text.RegularExpressions.Matches
Yourstring - ITNddMM1.zip + anything
Yourpattern - paste pattern I provided

Thanks

Hi

Welcome to uipath forum

You can do this in two ways

  1. With assign activity like this

Out_matches = System.Text.RegularExpressions.Regex.Matches(your input string.ToString,” ITNddMM1.zip[\d\D\w\W\n\s.]*”)

Where out_matches is a variable of type
System.Text.RegularExpressions.Matches

Or

  1. You can try using MATCHES activity with the same expression
    Here you go on how to use it

Cheers @Suraj_Das_IN

Thankyou .
i did try this solution but , it returns me of type matches which is of no use for my requirement. how can i use this in the following line of code to filter out those subjects which have the particular pattern.
Code used by me currently:

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x)x.Headers(“Subject”).Tostring.Contains(in_FilterByMailSubject)).ToList

Basically this is used by me to filter out mails from the mail box based on a particular subject where in_FilterByMailSubject is my input string containing the mail subject.
I want regex for this as the mail subject is always dynamic by having time in it as follows:
ITN14091.zip [Time: 13:56:30] the bold part can be anytime hence i wanted a regex to filter out but how can i incorporate the same in my code above.

Thankyou .
i did try this solution but , it returns me of type matches which is of no use for my requirement. how can i use this in the following line of code to filter out those subjects which have the particular pattern.
Code used by me currently:

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x)x.Headers(“Subject”).Tostring.Contains(in_FilterByMailSubject)).ToList

Basically this is used by me to filter out mails from the mail box based on a particular subject where in_FilterByMailSubject is my input string containing the mail subject.
I want regex for this as the mail subject is always dynamic by having time in it as follows:
ITN14091.zip [Time: 13:56:30] the bold part can be anytime hence i wanted a regex to filter out but how can i incorporate the same in my code.

So you want the list of mail message as output based on the variable in_filterByMailSubject
And if you want to use the Regex for that subject headers then mention like this

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x) System.Text.RegularExpressions.Regex.Match(x.Headers(“Subject”).Tostring,”your regex expression”).ToString.Contains(in_FilterByMailSubject)).ToList

Or if the Regex is not with subject you receive but with variable you have
Then

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x) x.Headers(“Subject”).Tostring.Contains(System.Text.RegularExpressions.Regex.Match(in_FilterByMailSubject,”your Regex expression”).ToString)).ToList

Cheers @Suraj_Das_IN

yes correct. the subject for today would be
ITN14091.zip [Time : anytime]
similarly for tom the mail subject will be:
ITN15091.zip [Time : anytime]

1 Like

Then this would work
Pls try and let know

Cheers @Suraj_Das_IN

@Suraj_Das_IN what did the variable in_FilterByMailSubject contains ?

as of now i was trying to use it as a string type variable with subject in it , but as it would be dynamic i cant understand how to use it, if it can be replaced with regex would be better i think

@Suraj_Das_IN I couldn’t understand that, you have a list of mail message and you Loop the subject of the mail’s and check if the subject had a pattern you are referring, where is thia variable in_FilterByMailSubject value coming from?

Thanks

yes i agree , there is no use of variable

@Suraj_Das_IN please try this,

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x) system.text.regularexpressions.regex.matches(x.Headers(“Subject”).Tostring,“.*.zip[\d\D\w\W\n\s.]*”,regexoptions.ignorecase).count > 0).ToList

Or

MailMssgs = MailMssgs.AsEnumerable().Where(Function(x) system.text.regularexpressions.regex.matches(x.Headers(“Subject”).Tostring,“[a-zA-Z]{3}\d{4,6}.zip[\d\D\w\W\n\s.]*”,regexoptions.ignorecase).count > 0).ToList

Both will work give a try

Thanks

@Suraj_Das_IN did that worked?

Thanks