How to I add a new line in a regex expression with Text Replace

I would like to use regex to add in an extra line in the output but \n does not seem to work. It treats this as an “n” character. Thanks.

Try this.

https://www.icloud.com/shortcuts/660357e110f94b9695702b44c07783d6

I am not sure I understand this. My apologies for being a newbie with shortcuts. I am very familiar with regular expressions. How do the text actions generate the variable names? I am trying to match a pattern at the beginning of a line

^([0-9])

and add a newline before the text:

\n$1

This is used to break up specific styled text into paragraphs. Thanks.

I tried changing the replacement pattern to a blank line with $1 in the second line and that appears to work. One followup question. I am trying to make a shortcut were I can highlight text in a text editor, run a shortcut on it and have it replace the text with the modified version. Is there a way to do this or should I create a shortcut that reads a file into a text variable, processes it and then writes it to a new version of the file? Thanks.

I believe the issue you were seeing with the RegEx is because /n only works in the search string not the replacement string.

With regard to the follow up question, one approach would be to configure a Shortcut to accept text input from the share sheet (in the per Shortcut settings) and as the last step in your Shortcut put the result in the pasteboard.

This would allow you to highlight your text in the text editor, share it with the shortcut and get the result in the pasteboard. You’d then need to manually paste this over the selected text.

Not perfect, but probably easier than writing to an intermediate file.

Note that the above is true for iOS and iPadOS, not sure if it also applies to Mac.

Thank you very much. I really appreciate the help.

When you reference other action steps in Shortcuts, they are referenced as something they term magic variables. When you add reference these in an action , you have the option to rename them. I renamed them in the example so you could easily verify which text entry was being used in the replace action where. I felt this was particularly important as you did not provide any test case or test expressions, and for another reason, covered below.

\n is not a valid token for the replace with element of the regular expression. This was the other driver for using a text action for the replace. Then the literal new line string can be passed in to the rteplacement.

What OS and editor app are you wanting to do this on, and do you have access to any other third automation tools? Shortcuts is just one tool, and there may be other tools you can apply to facilitate this. For example, if the editor was Drafts, it can call and get input back from a Shortcut. AppleScript is supported by many editors on macOS.

On the Mac, setting your Shortcuts as a Quick Action with output will allow you to access it from the context menu and insert/replace with the output of the Shortcut - simplified example below.

2022-05-22-06.37.49

This example simply replaces any selected text with “Hello World”.

Hope that helps.

Thanks. That works well. Thank you very much. If I use File action to open a text file, can I get access to the text in the file? Thanks again for your help.

Yes, the file action in Shortcuts will allow you to access the raw content of a file, as long as the file can be read as text.

Thanks. I was able to get a shortcut to work as a Quick Action

Receive Files input from Quick Actions
If there is no input:
Ask for Files

I then set a variable to the Shortcut input (which contained the text), modified it via regex pattern and was able to use the Save File action to output it. That action does not seem to let me control the name of the file so I had to add a Rename File action to handle that.

This lets me select a file in the Finder and use the quick action to create a modified version of the file.

Thanks again for your help.

You should be able to name the file using the Set name action.

1 Like

@Dave_Taenzer Yes, this is indeed a bug in Shortcuts. But fortunately, there is a super simple workaround. Just put all regex of the replacement string inside a variable, but replace all \n with real line breaks, and then use that variable in the text replacement command. That’s it. Works like a charm.

This is quite an old thread to resurrect, but may I ask where the reference to this being a bug is?

Not all regular expression engine implementations support tokens in substitutions, and I had understood the Workflow/Shortcuts implementation (based on the Apple ICU implementation) was one such case. I had thought I’d seen one of the Workflow team make reference to that being the case way back on Twitter, but I can’t find anything in my notes to confirm that so it could just be a poor recollection on my part. Assuming it is indeed a bug, I’d definitely like to make a note of the confirmation - particularly because it has been this for coming up on ten years now.

I have no reference to any documentation of this bug. I have, however, confirmed it myself. You are correct that support for token substitutions does vary among regex engines. But in this case, the support for the “\n” fluctuates. Sometimes it works, and sometimes it doesn’t. I have noticed that even as small changes as moving a Replace instruction up or down in the script affects this. Most of the time it is even non-reversible, and you need to remake the entire script to make it work again. These are clear signs of a buggy behavior.

Also, the token substitution actually does work if you follow the instructions in my workaround. Therefore, Shortcuts does have the actual functionally built into the Apple ICU. It is just unstable and buggy in its implementation …as so many other things are in Shortcuts.

In your workaround I understand you to be swapping out the \n for a true newline character. If so, that is not substituting with a token but rather substituting for a token - i.e. using the token for the matching only rather than using the token in the string being used as the substitute. That would be the same sort of approach as the one I posted originally as the example using an actual newline in the replaceme t string.

I would certainly agree that Shortcuts does seem to succumb to a larger that expected number of bugs, which is very frustrating when trying to build more sophisticated shortcuts, but I can’t say I’ve ever see a \n token work within the substitution of a regular expression based replace action. But, since I figured it wasn’t working after the first go I haven’t regularly tried it again, so th chances of me coming across it at random would b relatively small :man_shrugging:t2:

Starting from iOS 16.4, text fields in Replace Text (and many other actions) accept new lines; you can simply tap return to enter a new line

1 Like

Yes, you are quite correct @sylumer . This isn’t how I realized the token substitution had a bug. This is just the workaround. The fluctuating behavior was experienced by directly entering substitution tokens into the replacement field of the Replace command. And yes, my workaround is quite similar to your suggested approach. Except, I couldn’t make it work because the Shortcuts editor doesn’t accept newline characters in the replacement field/string. Not even copy-pasting them in there. It may have worked before, but it doesn’t anymore. Hence, the usage of a string variable.

(I should stress that I am using Shortcuts in macOS 14.3.1)

This is good news, and shows that Apple is aware of the Shortcuts shortcomings. Thx for updating us on that, @gluebyte :pray:

However, in the macOS version of Shortcuts (macOS 14.3.1) this doesn’t work, and is still a problem.

Wow. Thank you for that. How did you know?
I’ve had this problem occasionally for years and always think there is some peculiar RegEx for it but wind up doing some sort of new Text action work around. I think Apple and coders in general sometimes forget that people like me can easily program with Shortcuts because they don’t have to remember code syntax. I guess a new line entry isn’t particularly visible though and this is a weakness of the Action interface.

I keep track of changes to the WFActions.plist file in every iOS version (e.g. [iOS 16 → 17] New and updated actions) and found the Multiline key in many places in iOS 16.4:

{
   "is.workflow.actions.text.replace": {
      "Name": "Replace Text",
      "Parameters": [
         {
            "Label": "Find Text",
            "Multiline": true,
            ...
         },
         {
            "Label": "Replace With",
            "Multiline": true,
            ...
         },
         ...
1 Like