Extract ID from a mail Subject

Hi all, I want to get the Id from a mail subject in Imap and save in an attachment but it runs and return nothing. please check the screenshot


firstly I assume your email has the full message “Grow, Something New, and other are hiring” as one full string?

Secondly, do you have an example of the full subject you are trying to retrieve the employee number from as the above is a little unclear?

Grow, Something New, and other are hiring is the subject of the mail

hi @RPA-botDev

You want extract only number then use below code


While splitting it may return some null values. To ignore them use split function like this
String.Split("#".ToCharArray,StringSplitOptions.RemoveEmptyEntries) . This will remove empty entries.

Can you share a sample subject line?

The subject line is;
“Kaiti, #99453, please update your merger”
So, I want extract the ID 99453 from the above subject and save as attachment

Try this
(“Kaiti, #99453, please update your merger”.Split("#".ToArray,StringSplitOptions.RemoveEmptyEntries)(1)).Split(",".ToArray,StringSplitOptions.RemoveEmptyEntries)(0)

1 Like

If you split with “,” it could work but you need to be sure the subject will have the same format.

I got error index was outside the bounds of the array

Can you share the subject of the mail which you have error. I think the format changes for mail subject.

Oh, that’s true it changed;
“Special Offer: 20% off your first order!”
What if I want to get the 20%

Declare a variable “strInput” As String
Declare a variable “pFrom” As Integer
Declare a variable “pTo” As Integer
Declare a variable “result” As String

strInput = “Special Offer: 20% off your first order!”
pFrom = St.IndexOf("Special Offer: ") + "Special Offer: ".Length
pTo = St.LastIndexOf(“off your”)
result = St.Substring(pFrom, pTo - pFrom)

Try the above, Note: The code not tested

1 Like

What exactly are we trying to do here? Don’t you have a If Condition with the specific subject line? Or are you just trying to randomly get numbers from the subject lines?

You can also use Regex functions to achieve this
For previous subject use this “System.Text.RegularExpressions.Regex.Matches(“Kaiti, #99453, please update your merger”,”(?<=#)(.)(?=, )")(0).ToString"
For second subject sample use this “System.Text.RegularExpressions.Regex.Matches(“Special Offer: 20% off your first order!”,”(?<=: )(.
First find out the different formats of your mail subjects and then seperate them using if condition then write regex for each pattern.

Please do I need to assign the above regex expression to a variable with data type string

You can simply use it or if you need it in a variable you can store it in a string variable.

End of expression expected sir

Remove double quotes in beginning and end of the expression.

I got this error: specified argument was out of the range of valid values