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