Need Regex help! How can I select all the text on a specific line of a text file?

Good afternoon UiPath Forum,

I am trying to use a regex match where I pull all the characters on a specific line. Here’s the example. I have many files containing something like this. How can I use a regular expression to grab the text on the 3rd line (line 3) of the file. Some lines are empty. Some lines have text but line 3 always has values and those I need. Your assistance is greatly appreciated.

(Ignore the dashes. They are just for clarity here.)


This is the first line.

This is the third line.
This is the fourth line.

This is the sixth line.

Hi,

You can try below expression

thirdLineText = System.Text.RegularExpressions.Regex.Match(yourFileContent, “(?<=\n)([^\n]+)(?=\n[^\n]\n[^\n]$)”).Value

HI @jamiejam

you can try this regex expression …it will exactly select the 3 rd line and 6 line so on

for reference you can see the output :-

image

You can try this expression you will get the exact output

if you required only third line =
System.Text.RegularExpressions.Regex.Match(str,“(?<=[1]).*”,RegexOptions.Multiline).Value.Trim

if you required muiltiple of third lines 3,6,9 so on = System.Text.RegularExpressions.Regex.Matches(str_input,“(?<=[2]).*”,RegexOptions.Multiline)

str_input :
"This is the first line.

This is the third line.
This is the fourth line.

This is the sixth line.
This is the Seven line

This is the Nine line"

OutPut you need is :
1.This is the third line.
2.This is the sixth line.
3.This is the Nine line


@jamiejam


  1. \r?\n ↩︎

  2. \r?\n ↩︎

Let me know it’s working or not for you

Cheers… :slight_smile:

@jamiejam

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=^(.*\n){2}).*").Value

If you need to get the 6th line, please replace 2 with 5 in the above expression.

Regards,

Hi @jamiejam

Try using this expression:

strinput="This is the first line.

          This is the third line.
          This is the fourth line.

          This is the sixth line."

output= System.Text.RegularExpressions.Regex.Match(strinput,"(?<=^(.*\n){2})(.+)").Value

Note: strinput and output variables are of DataType System.String

Hope it helps

Thanks very much @Parvathy ! That was what I needed.

1 Like

You’re welcome @jamiejam

Happy Automation
Regards

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