Jump to content

How to insert a new line in a RegEx search and replace?


Recommended Posts

Posted

Some research tells me that Perl is a good scripting language to do this in, and I can find examples online, but I don't know how to work them into the right format for use with Alfred Workflows. This is what I have now, but it isn't working... (trying to replace "apple" with "pear" as a test)

 

$query = "{query}";

-p -e 's/apple/pear/g;' $query

print $query;

 

Posted

Found an example that made more sense to me. This works!

 

$query = "{query}";

my $find = "apple";
my $replace = "pear";
$find = quotemeta $find; # escape regex metachars if present

$query =~ s/$find/$replace/g;

print $query;

 

 

Posted

While that worked on a single line, I needed to change /g to /gm to do mult-line search. Then I needed to remove the "escape regex" line to be able to use more complex search operators. I also found that certain perl regex operators didn't seem to work for me. For instance instead of

^\s{4}-

 I had to enter

 

^    -

(with actual spaces).

 

I don't know why? I tested the above syntax on some online regex testing sites and it worked...

Posted

If you don't already know Perl, I'd recommend you use Ruby or Python or PHP or JavaScript. Basically, anything but Perl.

 

Almost nobody on the forum uses Perl, so you won't get much, if any, help.

 

What exactly are you trying to achieve?

Posted

If it helps, Alfred's 'Replace' workflow utility object can actually accept new lines if you alt-enter in the right field.

 

It's likely at some point I'll take a fresh look at all of the utility objects to see how they can be improved :)

 

Cheers,

Andrew

  • 5 years later...
Posted
On 7/25/2017 at 11:36 AM, Andrew said:

Alfred's 'Replace' workflow utility object can actually accept new lines if you alt-enter in the right field.

This is the most hidden feature I came across in a long time. It even remains invisible once you use it 😉

My experimenting did go as far as just hitting enter in the field on the right, but Alt-enter didn't occur to me. Thanks for sharing this here!

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...