Hi All,
i need regex for this

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.
Hi All,
i need regex for this

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:
System.Text.RegularExpressions.Regex.Match(inputText, pattern).Groups("FirstName").ValueSystem.Text.RegularExpressions.Regex.Match(inputText, pattern).Groups("LastName").ValueHi @Rakesh_Tiwari ,
You can try with following steps:
DMFIRSTNAME Name and DOB: FSLASTNAMEName DMFIRSTNAME FSLASTNAMERegex:
(?: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
![]()
![]()
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!
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.
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:
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!