Page 1 of 1

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

Posted: Fri Jun 14, 2019 10:46 am
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

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

Posted: Mon Jun 17, 2019 7:41 am
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".

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

Posted: Mon Jun 17, 2019 2:53 pm
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