Page 1 of 1

Regex Ignoring New Lines

Posted: Mon Jul 25, 2016 2:38 pm
by DtM
Hi guys,

I have a windows formatted text file (\r\n) that I am reading into a switch script.

Here is an example of the format:

Code: Select all

=======Section A=======
Value 1: I need this value
Value 2: I need this value
Value 3: I need this value

=======Section B=======
Value 4:
I need
all of
these values
Value 5: And this one too!
When I try to extract the text in value one using /Value 1\: (.+)/ it starts where I'd expect it to, but it continues on to the end of the file rather than to the end of the line.

I've tried converting the \r\n to \n as well as explicitly saying /Value 1\: (.+)\r\n/ or /Value 1\: (.+)$/ but it just isn't playing ball.

Does anyone have any idea how to get around this?

I am using var jobTicket = File.read(job.getPath() to get my text file into Switch, if that makes any difference.

Re: Regex Ignoring New Lines

Posted: Mon Jul 25, 2016 3:01 pm
by freddyp
Some other approaches you could try:
- File.readByteArray(job.getPath()) reads the file as a string of bytes rather than as text
- var fileObj = new File(job.getPath());
fileObj.open(File.ReadOnly);
var lines = fileObj.readLines();
fileObj.close()
lines is now an array of all the lines in the file and you can loop over that until your regex matches without considering line endings.
- use the "Text pickup" app: https://appstore.enfocus.com/product/TXT_pickup

Re: Regex Ignoring New Lines

Posted: Mon Jul 25, 2016 5:30 pm
by gabrielp
What system produces files like that? Seems like having Switch parse through a file like this reliably would be very challenging and prone to error.

Re: Regex Ignoring New Lines

Posted: Tue Jul 26, 2016 9:02 am
by DtM
gabrielp wrote:What system produces files like that? Seems like having Switch parse through a file like this reliably would be very challenging and prone to error.
Tell me about it. :lol: I've no idea what the system is, I believe it's something bespoke our client had built for them by a third party, but I've been assured that the data is being generated automatically and all of the headings will be present all of the time...but that remains to be seen!

Re: Regex Ignoring New Lines

Posted: Tue Jul 26, 2016 3:36 pm
by gabrielp
Well, since I'm not very good at writing RegEx, I would opt for making a function which would parse this document and return an object. It would parse the document as an array, with each array item as a line. I would then loop through the array and construct a Javascript object (or XML if you need Switch to keep it as a dataset) and return it. Essentially what Freddy said. My post in this thread has similar code: https://forum.enfocus.com/viewtopic.php?f=13&t=1473