How do I extract all information from point A to point B

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.

Hi @Sundez

System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=\n)\d+.*\s+.*\s+.*)”)

image

Hope it helps!!

Hi @Sundez
Use the below Regex expression:

[\d.]+\s+\w(.*)\n(.*)\n.*

Regards,

Hi @Sundez

(?m)^1.2.3..$(?:\r?\n(?:(?!testB).$))*

@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())

@Sundez

Use “replace activity” to replace the text

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)

@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,

Hi @Sundez

Take an assign activity

Matches=System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=\n)\d+.\s+.\s+.*)")

or use Find matching patterns

image

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”)

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!!

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
image

Hope this helps!!!

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 ?

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!!

@Sundez

Str_Replace=Str_data.Replace(“testA”,“”)
Str_Out=Str_Replace.Replace(“testB”,“”)

Hope this helps