Hi, I have a text in notepad and need to convert that data into an array and need to scrape Relevant data using Regex. Please help me.
The text file Contains data like below
…
Identifier-0
Type-456
Sender-ABCD
Receiver-xyz
Date-2-February-2023
Time - 00:00:19
File Type - A
TRNumber - 12345
RR - 098765
Narrative
GST: GUARANTEE
From the above text i need Type, Sender, Receiver, Date, Time, TRNumber, RR and GST.
@Sujitha_A - Hi Sujitha, as @arjunshenoy mentioned you can use Read Activity followed by split operation to convert text into array and then performing Regex operations. I am also attaching my solution in steps:
Read the text file using the “Read Text File” activity in UiPath and store the output in a variable, let’s say “textData”.
Split the “textData” variable using the “Split” function with delimiter as “newline” to convert the text data into an array. Store the output in a variable, let’s say “dataArray”.dataArray = textData.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Use the “Matches” activity in UiPath to scrape relevant data using regular expressions.For example, to extract the Type, Sender, Receiver, Date, Time, TRNumber, RR, and GST values from the “dataArray” array, you can use the following regular expressions:
typeRegex = “(?<=Type-)\d+”
senderRegex = “(?<=Sender-)[A-Za-z]+”
receiverRegex = “(?<=Receiver-)[A-Za-z]+”
dateRegex = “(?<=Date-)\d{1,2}-[A-Za-z]±\d{4}”
timeRegex = “(?<=Time - )\d{2}:\d{2}:\d{2}”
trNumberRegex = “(?<=TRNumber - )\d+”
rrRegex = “(?<=RR - )\d+”
gstRegex = “(?<=Narrative\s*).*”
Loop through the “dataArray” array using the “For Each” activity in UiPath.
Inside the loop, use the “Matches” activity with the respective regular expressions to extract the relevant data and store it in variables.
Store the extracted data in a data table or any other data structure as per your requirement.
Or if this looks like the long way use assign activity and inside there you can use the following
Regex.Match(text,“Type-(.)”).Value.Trim
Regex.Match(text,“Sender-(.)”).Value.Trim
Regex.Match(text,“Receiver-(.)”).Value.Trim
Regex.Match(text,“Date-(.)”).Value.Trim
Regex.Match(text,“Time - (.)”).Value.Trim
Regex.Match(text,“TRNumber - (.)”).Value.Trim
Regex.Match(text,“RR - (.)”).Value.Trim
Regex.Match(text,“GST:(.)”).Value.Trim
@Sujitha_A You need to loop the extracted array from point 2 and then inside that you should use matches activity along with the regex to extract required value. Also please make sure, these regex identifier should be kept in Config file so that you can alter them in future without making code changes.