Regex Ignoring New Lines

Post Reply
DtM
Member
Posts: 27
Joined: Tue Aug 04, 2015 3:02 pm

Regex Ignoring New Lines

Post 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.
freddyp
Advanced member
Posts: 1031
Joined: Thu Feb 09, 2012 3:53 pm

Re: Regex Ignoring New Lines

Post 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
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Regex Ignoring New Lines

Post 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.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
DtM
Member
Posts: 27
Joined: Tue Aug 04, 2015 3:02 pm

Re: Regex Ignoring New Lines

Post 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!
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Regex Ignoring New Lines

Post 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: viewtopic.php?f=13&t=1473
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Post Reply