Sundez
(Sundez)
July 20, 2023, 6:39am
1
Hi. I’m very new to UiPath.I would really appreciate any guidance. Please kindly help
I got a task to extract information from text file.
input are string as
“testA
1.2.3. Product type
productA 5(4-5-6)
productB 3(1-8-9)
testB”
If I have to get output as string as
“1.2.3. Product type
productA 5(4-5-6)
productB 3(1-8-9)”
What would be a best way to approach it ? Split text ? Regex? I tried both but doesn’t seem to make anything work.
If possible I also want to put above output into datatable later.
Thank you very much for reading.
mkankatala
(Mahesh Kankatala)
July 20, 2023, 6:41am
2
Hi @Sundez
System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=\n)\d+.*\s+.*\s+.*)”)
Hope it helps!!
Parvathy
(PS Parvathy)
July 20, 2023, 6:43am
3
Hi @Sundez
Use the below Regex expression:
[\d.]+\s+\w(.*)\n(.*)\n.*
Regards,
rlgandu
(Rajyalakshmi Gandu)
July 20, 2023, 6:44am
5
@Sundez
input_string = “”“testA
1.2.3. Product type
productA 5(4-5-6)
productB 3(1-8-9)
testB”“”
Find the indices of “testA” and “testB”
start_index = input_string.find(“testA”)
end_index = input_string.find(“testB”) + len(“testB”)
Extract the text between “testA” and “testB” (including “testA” and “testB”)
text_between_testA_testB = input_string[start_index:end_index]
Replace the extracted text with an empty string in the input_string
output_string = input_string.replace(text_between_testA_testB, ‘’)
print(output_string.strip())
rlgandu
(Rajyalakshmi Gandu)
July 20, 2023, 6:47am
6
@Sundez
Use “replace activity” to replace the text
Sundez
(Sundez)
July 20, 2023, 6:48am
7
Thank you very much for your reply. How do I use your code you give me? By apply to assign activity ? or do I have to use activity match in studio ? Which one would be best practice?
Thank you.
Hi @Sundez
Split(Split(“testA 1.2.3. Product type productA 5(4-5-6) productB 3(1-8-9) testB”,“testA”)(1),“testB”)(0)
Parvathy
(PS Parvathy)
July 20, 2023, 6:49am
9
@Sundez
You can use “Find Matching Patterns” activity.
input = "testA
1.2.3. Product type
productA 5(4-5-6)
productB 3(1-8-9)
testB"
Pattern: "[\d.]+\s+\w(.*)\n(.*)\n.*"
Hope it helps!!
Regards,
lrtetala
(Lakshman Reddy)
July 20, 2023, 6:50am
10
Hi @Sundez
Take an assign activity
Matches=System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=\n)\d+.\s+. \s+.*)")
or use Find matching patterns
I hope it helps!!
Hi @Sundez
The datatype of outputText
should be System.Text.RegularExpressions.MatchCollection
outputText = System.Text.RegularExpressions.Regex.Matches(inputText, “(?m)^1.2.3..$(?:\r?\n(?:(?!testB). $))*”)
Use For each and access the output with current item
Hope it helps!!
Hey @Sundez
outputText = System.Text.RegularExpressions.Regex.Matches(inputText,“(?<=testA)(\n.+)*”).Value.ToString.Remove(“testB”)
mkankatala
(Mahesh Kankatala)
July 20, 2023, 6:53am
13
Okay @Sundez
First create a variable and store the Input data in a String datatype Variable.
Create a variable with IEnumerable of Matches datatype.
In the assign activity give the variable in Save to field, In the value to save give the below expression.
- Assign -> MatchVariable = System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=\n)\d+.*\s+.*\s+.*)”)
Check the below workflow it works for me
If you have any queries let me know.
Hope it helps!!
supriya117
(Supriya Allada)
July 20, 2023, 7:04am
14
Hi @Sundez
Try this:
var1 = “testA
1.2.3. Product type
productA 5(4-5-6)
productB 3(1-8-9)
testB”
res = var1.Remove(var1.Length-6).Substring(6)
Next use generate text from datatable activity to convert this text to datatable.
Hey @Sundez ,
outputText = System.Text.RegularExpressions.Regex.Matches(inputText,“(?<=testA)(\n.*)+(?=testB)”).Value.ToString
Hope this helps!!!
Sundez
(Sundez)
July 20, 2023, 7:54am
16
mkankatala:
((?<=\n)\d+.*\s+.*\s+.*)
Thank you very much.
May I ask why does we have to replace extracted text with an empty string ?
What does .strip() help us in this situation ?
mkankatala
(Mahesh Kankatala)
July 20, 2023, 8:31am
17
Welcome @Sundez
If we want to remove the spaces between the string we use this replace function to replace with nothing.
Ex - Sub String in this I want output is SubString in this we use replace function (“Sub String”.replace(" “,”") then output is SubString
If you find the solution, Make mark it as solution which helps other…
Hope you understand!!
rlgandu
(Rajyalakshmi Gandu)
July 20, 2023, 8:31am
18
@Sundez
Str_Replace=Str_data.Replace(“testA”,“”)
Str_Out=Str_Replace.Replace(“testB”,“”)
Hope this helps