Regex 8 characters from Mail body

I need to fetch 8 characters from Merchant Code column from mail body, how can i do it?

Hi @Mayur_N

Could you try the below one,

(?<=Queries\s)[A-Za-z]+
- Assign -> Output = System.Text.RegularExpressions.Regex.Match(VariableName.toString,"(?<=Queries\s)[A-Za-z]+").Value

Pass the Variable in the place of VariableName.

Hope it helps!!

No, It is not working.

I need to fetch that JZpTukoW (8 character string) in the column of Merchant Code.

@Mayur_N

  1. Get IMAP Mail Messages (or Get Outlook Mail Messages)

    • Output: mailMessages (List)
  2. For Each mailMessage in mailMessages
    a. Assign
    - emailBody = mailMessage.Body

    b. Matches
    - Input: emailBody
    - Pattern: (?<=Merchant Code: ).{8}
    - Result: matches (MatchCollection)

    c. For Each match in matches
    i. Assign (or Print)
    - merchantCode = match.ToString()

Could you share me the mail body in a text format… @Mayur_N

Then it’s possible to write the regex for that.

If you want regex for 8 characters check the below one,

[A-Za-z]{8}

Hope you understand!!

To extract data, such as a “Merchant Code,” from an email body in UiPath, you can follow these steps:

  1. Use a Get Mail activity to retrieve the emails and save them to a variable.
  2. Iterate through the emails using a For Each activity with the Type Argument set to System.Net.Mail.MailMessage.
  3. Inside the loop, use an Assign activity to get the body of the email as a string (e.g., mailBody = item.Body.ToString).
  4. If the “Merchant Code” follows a consistent pattern, you can use string manipulation methods like IndexOf() and Substring() to extract it. For example:
If mailBody.Contains("Merchant Code:") Then
    Dim startIndex As Integer = mailBody.IndexOf("Merchant Code:") + "Merchant Code:".Length
    Dim endIndex As Integer = mailBody.IndexOf(Environment.NewLine, startIndex)
    Dim merchantCode As String = mailBody.Substring(startIndex, endIndex - startIndex).Trim()
End If
  1. If the email body is in HTML format and the data is in a table, you might need to convert the HTML to text or use an HTML parser to extract the table data.

Hi @Mayur_N ,

You can do this as below

Workflow:

  1. Assign1: It Will remove the new line from mail body
System.Text.RegularExpressions.Regex.Replace(mail.Body, "\r\n", " ")
  1. Assign2: It will remove the extra spaces from words
System.Text.RegularExpressions.Regex.Replace(Mail_body.Trim, "\s+", " ")
  1. Regex to extract the Merchat Code
System.Text.RegularExpressions.Regex.match(Mail_body.Trim, "(?<=Queries\s)(.*?)[\s]").ToString.Trim

Output:
image

Hope it helps you :slight_smile:

Regards,
Vinit Mhatre