How to get line before and after specific line in text file

Hi all, I am currently working with a pdf copy of an invoice template and saving it as a .txt file to read. Anchor Base, Get Text, Get Full Text and Screen Scraping methods did not work for me when trying to read the specific area of text in the invoice.

I would like to know how I can get “11 Test Road” and “The Test Apartment” from the below example. Line 1, 3 and 4 are variables that change depending on the invoice. The only variables that do not change are “Person Address” and “Country”. What kind of string manipulation should I use?
Is Regex necessary for this?

image

@anonymous3 - Please try the below Regex Patterns…

  1. 11 Test Road

  1. The Test Apartment

1 Like

Hi @anonymous3
use this regex to extract the 11 Test Road
[\s\S]+(?=Person Address)
image

For extracting the the test apartment use below
.*(?=\nCountry)
image

Hi @prasath17 thank you for the quick reply. I am not familiar with regex in uipath studios, could I ask you to provide a screenshot or .xaml file to show how it looks like in studios?

Hi @NIVED_NAMBIAR thank you for the quick reply. I am not familiar with regex in uipath studios, could I ask you to provide a screenshot or .xaml file to show how it looks like in studios?

1 Like

@anonymous3 - Please find the starter help …Regex Pattern link

StrMatches is of Variable type : MatchCollection

Write Line: StrMatches(0).groups(1).tostring + vbcr +StrMatches(0).groups(2).tostring

Note: you can assisgn StrMatches(0).groups(1).tostring to get the 1st line and StrMatches(0).groups(2).tostring to get the apartment line…

Hope this helps…

@anonymous3

Welcome to the Community!

If the labels are going to change and the position is going to same then use below logic.

Considering varStr is your string

  1. First line
    varStr.Split(System.Environment.NewLine.ToCharArray)(0)

  2. Last second line
    varStr.Split(System.Environment.NewLine.ToCharArray)(varStr.Split(System.Environment.NewLine.ToCharArray).Length-2)

1 Like

Hi @prasath17 ,

Thank you so much for your help! My process works with this :slight_smile:

Cheers

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.