Regexp (or other solution) for replacing double quotes with curly quotes in text?

Shane's icon

I know there are tons of forum discussions about regexp, and many of them deal with quotes and the way Max deals with them, but I'm unable to find anything that exactly fits...

I am using [textedit] to enter text and then using [text] to write it to a txt file. Later, I am compiling all the txt files into a new txt file. Everything works fine, with one little exception: if there are quotation marks in one of the first set of txt files (the ones written in [textedit]), those quotation marks get eliminated once I compile the new txt file. But if I have a set of txt files that use curly quotes, these don't get stripped when I compile them.

My question: is it possible to use [regexp] or some other object (I was also experimenting, without luck, with [string.replaceall]) to replace all "straight" double quotes entered in [textedit] with "curly" ones?

(I was trying to do something like this: https://stackoverflow.com/questions/71029975/regex-to-replace-regular-quotes-with-curly-quotes -- but Max doesn't like the quotation marks...)

Thanks in advance!

Shane's icon

PS: If it's impractical to change the output from [textedit] on the fly, I'd also be happy with a solution that could do the same for txt files outputted from [text] after the fact.

Thanks!

Roman Thilenius's icon

you can hardly send ""test"" between max objects.

additional leading or ending spaces can help. then the string is treated like symbols with only a number as argument, and the quote characters are part of the symbol...

the question is how long it remains like that in other max objects (which would be required to remove the space again.)

or consider postprocessing your curlies or >><<`s in bbedit...

Shane's icon

Yeah, that's not very promising, is it?

I can't do postprocessing, because I need this all to be self-contained for an end user who is not me.

In addition to regexp and other methods, I would be open to wrapping javascript into the patch, if that's an option, but I don't know how to go about it.

Any tips appreciated!

Peter Ostry's icon

Can't you swap the quotes for something that doesn't hurt?


Shane's icon

Ah, so simple. I think single quotes (ASCII 39) should work, right?

Thank you!

Shane's icon

Hmmm... When I do this (routing output from textedit over to text with these steps between) it works to replace double quotes with single ones, but it strips out all the commas from the text... Any tips?

Peter Ostry's icon

I wouldn't use anything that has a meaning in Max or RegEx or programming languages in general. No double or single quotes, no dots, commas, colons, brackets, plus/minus, no slashes and so on. And you better don't try to fool the system by using accents because they look like single quotes, or inch signs because they look like quotes. A lot of software interprets them because they look equal.

I did a lot of text conversions and found that the hash sign (#) is one of the safest. You may use combinations of signs like #x#<your text>#x# or such, but that may complicate the final "decoding". It is practically impossible to write the perfect conversion for everything. You can just optimize it for your data and data flow.

You mentioned that someone else is going to read or process the file. If you exactly know what the receiver does with the text, it would be best to write the quotes to the file with ASCII numbers that work for the client. Same method as the quote substitution above, but reversed and maybe with different numbers. You would do this at the very last stage before you write the text to the file. Maybe you cannot test the result in Max, but I guess the important thing is that the client can use it and you should be able to reproduce the client’s steps in advance.

Btw – if the text finally goes to another operating system, also pay attention to the "new line" method. Carriage Return (cr, 13) or Line Feed (lf, 10) or both.

Source Audio's icon

as soon as you output text from text object,

you are dependant on max treatment of this chars like quotas, comma, semicolon etc.

You started with quotas question, now commas get into play ...

is it that you want to merge text files, from a different thread ?

If that are text files you can merge them using filein - fileout or writebytes.js

without loosing any formatting.

P.S.

or better using shell and one of many commands that can do so

Roman Thilenius's icon

single quotes do the same in max - actually back in the days it all started with single quotes.

allowing to replace quotes with something else in the text output is your best bet (unless you use javascript to produce the textfiles, i had that in mind but dont know it either.)

Shane's icon

Thank you, all! This is all very helpful -- I am still new to Max and didn't quite understand that Max treatment would take over as soon as I output from the text object. This is of course indispensable knowledge.

@Source Audio: Yes, this is related to the other thread, which I thought I had sorted out with your generous help. And it does in fact work fine so long as no double quotes are in play -- but quotation marks are in fact pretty important, which is why I thought an earlier intervention (between textedit's output and its encoding via the text object) could solve the problem. It sounds like I shouldn't mess with that step, however, and instead shift the strategy at the end, when all the text files get combined.

You mention several possible solutions: 1. filein/fileout (I was reading on the forums about the lack of an official fileout and a user-created external). 2. writebytes.js (do you mean using this js method via the File object?). 3. shell along with a command (is there a good command that would work on both Mac and Windows?).

Here is what I want to do: I have a folder full of text files, each sequentially named (filename_1.txt, filename_2.txt, etc). I won't know in advance how many there will be. I want to combine them, in order, into one file while preserving all formatting. I would also like to add some kind of separator (for example, a short line of dashes: --------------) between the text of each, and add some info (including title and timestamp) at the very top of the file. I am attaching an example of a compiled text (which combines 4 txt files but removes quotation marks) along with one of the original source txt files (the first of the 4) to give you a better idea.

My question: which of the the solutions outlined (filein/fileout, writebytes.js [File object or v8?], or shell) seems most promising for this use case?

Thanks so much for your help with this! I am learning a lot--including how much more there is to learn...

intro.mp4_film|minutes_2025-02-17_21-09-26.txt
txt
intro.mp4_1.txt
txt

Shane's icon

Update: I was able to get it to work with [v8] and some help from ChatGPT to write a js file, which I am attaching in case it is of interest. Now I just pass it an input folder to read txt files from, an output location to write to, and the name of a file to use in naming the output, followed by the command to execute the central function (combineTextFiles). It works!

Thanks for all the help here!

combineTxt.js
js

Shane's icon

Just for completeness, updating the js file here with some errors corrected:

combineTxt.js
js