regexp for splitting a string based on \r\n

If I use a 2-line item description, but want to actually make it into 4 lines by manually adding \r\n (or something like that) into the description, what would be the command to split it into a 4-element array?


For example:


Item description is:


This is the first line\r\nof a long description


This is the second line\r\nof a long description


The two lines are separated by \r\n even though you can't see it.


if I say: p description.split(/[\r\n]+/)


I get this:


["This is the first line\\r\\nof a long description","This is the second line\r\nof a long description"]


What I want is this:


["This is the first line","of a long description","This is the second line","of a long description"]


I've tried using gsub to change "\r\n" to /\r\n/ but in addition to not working, it also messed up the actual line break that was already there.


btw - I know this will not be shown as a 4-line description by any of the RTP scripts. I am using it for a different purpose.
Just curious from the first test so... did you try doing it like this?

.split(/[\\r\\n]+/)
Yes - that split it on every r and every n, not \r and \n


So I ended up with this:


["This is the fi", "st li", "e", "of a lo", "g desc", "iptio", "\r\nThis is the seco", "d li", "e", "of a lo", "g desc", "iptio"]


Not quite the result I was after, either 🙂
What you wrote works fine for me

Code:
s = "This is the first line\r\nof a long descriptionThis is the second line\r\nof a long description"p s.split(/[\r\n]+/)
result

Code:
["This is the first line","of a long description","This is the second line","of a long description"]
I get that if I put the text straight into the script, but not if I set it up as the item description.


As a text string within the script, it works with the original command: p s.split(/[\r\n]+/) without needing the | symbol too.


Edit: okay, this works ...

p description.gsub("\\r"){"\r"}.gsub("\\n"){"\n"}.split(/[\r\n]+/)Now the question is ... is there a way to make it less verbose?
Of course, the other alternative is just to stick the descriptions and all the other stuff into the script itself and not try to read it from items, but that makes it less friendly for sharing.
I've run into this problem as well and decided to just put a pipe symbol "|" instead since it's easier to find and insert the line break. Really any character would work that isn't commonly used.

edit:

I was trying to remember the issue as it's been a while since I've done any coding. It had something to do with the draw_text_ex method and how it arranges the text. It does it's own methods for line breaks and such that made it hard to gsub those in
Oh, that's nice.


This:


This is the first line|of a long description


This is the second line|of a long description


and this:


p description.split(/[\|\r\n]+/)


will then produce this:


["This is the first line","of a long description","This is the second line","of a long description"]


This is pretty good, and actually helps with another issue I'd started to think about.


Thanks for your help everyone 🙂

Original thread: https://forums.rpgmakerweb.com/threads/regexp-for-splitting-a-string-based-on-r-n.37159/