[SOLVED] $regext() for reading out n-th line

Questions and answers on how to get the most out of FFAStrans
Post Reply
ThomasM
Site Admin
Posts: 231
Joined: Wed Feb 22, 2017 6:36 am

[SOLVED] $regext() for reading out n-th line

Post by ThomasM »

Hi folks,

one for the weekend :roll:

I got a .TXT with three lines:

unknown
unknown
pcm_s16le

and want to read out line 1, 2 and 3 separately for populating variables. For example line 3, I tried

(?im)(?:\r\n?|\n){2}(\w+) and
(?:\r\n?|\n){2}(\w+) and
\n\n(\w+) and so on...

but it returns "unknown" instead of "pcm_s16le". I tried several regex-tester (https://www.regexpal.com/ and https://regexr.com/ for example) and cruised through several forums for getting this running - but no success.

Any idea?
thanks in advance,

tom
Last edited by ThomasM on Mon Jun 17, 2019 2:54 pm, edited 1 time in total.
emcodem
Posts: 1811
Joined: Wed Sep 19, 2018 8:11 am

Re: $regext() for reading out n-th line

Post by emcodem »

Hi Thomas,

this is a chance to proof that my tipp with the ".*?" from your last regex thread is actually useful. Sure there might be many ways to get this done, this is just one possible way.
All you need to know is which bytes are used for the Newline in your text document. There are basically only 2 options:
"\n"
"\r\n"

Also we need to use parantheses () to define which line we want to match, e.g.

First line

Code: Select all

(.*?)\r\n
In words: match anything up to the occurence of line feed and new line character but do not include line feed and new line in the match result

Second line

Code: Select all

.*?\r\n(.*?)\r\n
Third line

Code: Select all

.*?\r\n.*?\r\n(.*?)

full example used in Populate variables processor to match second line:

Code: Select all

$regext($read("c:\temp\newline.txt"),".*?\r\n(.*?)\r\n")
As said, depending on your text file, you might need to replace all "\r\n" by just "\n".
emcodem, wrapping since 2009 you got the rhyme?
ThomasM
Site Admin
Posts: 231
Joined: Wed Feb 22, 2017 6:36 am

Re: $regext() for reading out n-th line

Post by ThomasM »

Hi emcodem,

thank you for the input.

As always - works great. I´m trying hard to get this regex-thing working for me and many small expressions I can do on my own. But the more complex ones...

Now as I see it I understand it. Once again - Thank you!

regards,
tom
Post Reply