Need Regex expression for this

Hi All,

i need regex for this
image
i want to get Firstname and Lastname, due to system glitch 90% time it will come like this but in 10 % it will come like Name DMFIRSTNAME FSLASTNAME
i need regex for both the cases.

Try this out - ^(?:Name\s+(?\S+)\s+(?\S+)|(?.+?)Name and DOB:\s*(?\S+))

To extract the values using a Matches or Is Match / Matches activity or in Assign activities:

  1. First Name: System.Text.RegularExpressions.Regex.Match(inputText, pattern).Groups("FirstName").Value
  2. Last Name: System.Text.RegularExpressions.Regex.Match(inputText, pattern).Groups("LastName").Value

Hi @Rakesh_Tiwari ,
You can try with following steps:

  1. DMFIRSTNAME Name and DOB: FSLASTNAME
  2. Name DMFIRSTNAME FSLASTNAME

Regex:

(?:Name\s+)?(?DM[A-Z]+)(?:\s+Name\s+and\s+DOB:\s+|\s+)(?FS[A-Z]+)

UiPath example:
System.Text.RegularExpressions.Regex.Match(
inputText,
“(?:Name\s+)?(?DM[A-Z]+)(?:\s+Name\s+and\s+DOB:\s+|\s+)(?FS[A-Z]+)”
)

Get values:
firstName = match.Groups(“FirstName”).Value
lastName = match.Groups(“LastName”).Value

Thanks

Hi @Rakesh_Tiwari,

You can use the below Regex for First name and last name. It will work for both the cases:

firstName = System.Text.RegularExpressions.Regex.Match(inputText, “DM([A-Za-z]+?)(?:Name\b|\s+FS)”).Groups(1).Value

lastName = System.Text.RegularExpressions.Regex.Match(inputText, “FS([A-Za-z]+)”).Groups(1).Value

Thanks!

i have tried this expression, but getting error
System.Text.RegularExpressions.Regex.Match(var_str_WorkItemDetails, “[\w]+) And DOB:\s*(?[\w]+)”).Groups(1).Value

it is better to not to use DM or FS because it will keep on changing
@Jasmin_Bar

my doubt is what if DM and FS is not in everycase, how regex will work then

Hi @Rakesh_Tiwari,

Can you provide a few sample of data?

Thanks!

image
image
so this dynamic

Hi @Rakesh_Tiwari,

If Name and DOB text is fixed, you can use the below expression:

firstName = System.Text.RegularExpressions.Regex.Match(inputText, "^[A-Za-z]{2}([A-Za-z]+?)Name\b").Groups(1).Value
lastName = System.Text.RegularExpressions.Regex.Match(inputText, "DOB:\s*[A-Za-z]{2}([A-Za-z]+)\s*$").Groups(1).Value

Thanks!

Multiple Assign - Extract Workitem Details: Can not assign ‘System.Text.RegularExpressions.Regex.Match(var_str_WorkItemDetails, “([1]{2}([A-Za-z]+?)Name\b”).Groups(1).Value’ to ‘var_str_FirstName’.

got above error, expression not working


  1. A-Za-z ↩︎

You can handle both formats using a conditional Regex approach.

For the normal format:

John Smith

use:

^(?<FirstName>\S+)\s+(?<LastName>\S+)$

For the format with the prefixes:

DMJohn FSmith

use:

^DM(?<FirstName>\S+)\s+F(?<LastName>\S+)$

In UiPath, you can first check whether the input starts with DM and then apply the corresponding Regex. This will make the extraction of FirstName and LastName more reliable for both cases.

Hi @Rakesh_Tiwari

Use this regex:

(?:[A-Z]{2}FIRSTNAME|[A-Z]{2}LAST(?:NAME|AME))([A-Za-z]+)

In UiPath, use Matches and take Groups(1).Value.

For example:

TXFIRSTNAMEJohn and DOB: USLASTAMESmith

it returns:

  • John
  • Smith

Hi @Rakesh_Tiwari,

Can you check your variable type. I am not facing issue with the expression.

Thanks!

Hi @Rakesh_Tiwari,

You can try this expression:
[A-Z]{2}(?<FirstName>(?:(?!Name)[A-Za-z])+).*?[A-Z]{2}(?<LastName>(?:(?!Name)[A-Za-z])+)

This expression finds a 2 uppercase-letter code, then grabs the name right after it.

Hope this works for your case.

Cheers!